Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Ntfy_archive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Ntfy_archive
Commits
40fbce07
Commit
40fbce07
authored
3 years ago
by
Philipp Heckel
Browse files
Options
Downloads
Patches
Plain Diff
Test for simple pub sub
parent
aa976484
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
Makefile
+1
-1
1 addition, 1 deletion
Makefile
README.md
+4
-0
4 additions, 0 deletions
README.md
docs/publish.md
+1
-1
1 addition, 1 deletion
docs/publish.md
server/server.go
+9
-4
9 additions, 4 deletions
server/server.go
server/server_test.go
+65
-4
65 additions, 4 deletions
server/server_test.go
with
80 additions
and
10 deletions
Makefile
+
1
−
1
View file @
40fbce07
...
@@ -72,7 +72,7 @@ coverage-upload:
...
@@ -72,7 +72,7 @@ coverage-upload:
# Lint/formatting targets
# Lint/formatting targets
fmt
:
fmt
:
$(
GO
)
fmt
./..
.
gofmt
-s
-w
.
fmt-check
:
fmt-check
:
test
-z
$(
shell gofmt
-l
.
)
test
-z
$(
shell gofmt
-l
.
)
...
...
This diff is collapsed.
Click to expand it.
README.md
+
4
−
0
View file @
40fbce07
...
@@ -2,6 +2,10 @@
...
@@ -2,6 +2,10 @@
# ntfy.sh | Send push notifications to your phone or desktop via PUT/POST
# ntfy.sh | Send push notifications to your phone or desktop via PUT/POST
[

](https://github.com/binwiederhier/ntfy/releases/latest)
[

](https://github.com/binwiederhier/ntfy/releases/latest)
[

](https://pkg.go.dev/heckel.io/ntfy)
[

](https://github.com/binwiederhier/ntfy/actions)
[

](https://goreportcard.com/report/github.com/binwiederhier/ntfy)
[

](https://codecov.io/gh/binwiederhier/ntfy)
[

](https://gophers.slack.com/archives/C01JMTPGF2Q)
[

](https://gophers.slack.com/archives/C01JMTPGF2Q)
**ntfy**
(pronounce:
*notify*
) is a simple HTTP-based
[
pub-sub
](
https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
)
notification service.
**ntfy**
(pronounce:
*notify*
) is a simple HTTP-based
[
pub-sub
](
https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
)
notification service.
...
...
This diff is collapsed.
Click to expand it.
docs/publish.md
+
1
−
1
View file @
40fbce07
...
@@ -279,7 +279,7 @@ Here's an **excerpt of emojis** I've found very useful in alert messages:
...
@@ -279,7 +279,7 @@ Here's an **excerpt of emojis** I've found very useful in alert messages:
</td>
</td>
</tr></table>
</tr></table>
You can set tags with the
`X-Tags`
header (or any of its aliases:
`Tags`
, or
`ta`
). Specify multiple tags by separating
You can set tags with the
`X-Tags`
header (or any of its aliases:
`Tags`
,
`tag`
,
or
`ta`
). Specify multiple tags by separating
them with a comma, e.g.
`tag1,tag2,tag3`
.
them with a comma, e.g.
`tag1,tag2,tag3`
.
=== "Command line (curl)"
=== "Command line (curl)"
...
...
This diff is collapsed.
Click to expand it.
server/server.go
+
9
−
4
View file @
40fbce07
...
@@ -306,17 +306,22 @@ func parseHeaders(header http.Header) (title string, priority int, tags []string
...
@@ -306,17 +306,22 @@ func parseHeaders(header http.Header) (title string, priority int, tags []string
priority
=
1
priority
=
1
case
"2"
,
"low"
:
case
"2"
,
"low"
:
priority
=
2
priority
=
2
case
"3"
,
"default"
:
priority
=
3
case
"4"
,
"high"
:
case
"4"
,
"high"
:
priority
=
4
priority
=
4
case
"5"
,
"max"
,
"urgent"
:
case
"5"
,
"max"
,
"urgent"
:
priority
=
5
priority
=
5
default
:
default
:
priority
=
3
priority
=
0
}
}
}
}
tagsStr
:=
readHeader
(
header
,
"x-tags"
,
"tags"
,
"ta"
)
tagsStr
:=
readHeader
(
header
,
"x-tags"
,
"tag"
,
"tags"
,
"ta"
)
if
tagsStr
!=
""
{
if
tagsStr
!=
""
{
tags
=
strings
.
Split
(
tagsStr
,
","
)
tags
=
make
([]
string
,
0
)
for
_
,
s
:=
range
strings
.
Split
(
tagsStr
,
","
)
{
tags
=
append
(
tags
,
strings
.
TrimSpace
(
s
))
}
}
}
return
title
,
priority
,
tags
return
title
,
priority
,
tags
}
}
...
@@ -325,7 +330,7 @@ func readHeader(header http.Header, names ...string) string {
...
@@ -325,7 +330,7 @@ func readHeader(header http.Header, names ...string) string {
for
_
,
name
:=
range
names
{
for
_
,
name
:=
range
names
{
value
:=
header
.
Get
(
name
)
value
:=
header
.
Get
(
name
)
if
value
!=
""
{
if
value
!=
""
{
return
value
return
strings
.
TrimSpace
(
value
)
}
}
}
}
return
""
return
""
...
...
This diff is collapsed.
Click to expand it.
server/server_test.go
+
65
−
4
View file @
40fbce07
...
@@ -17,18 +17,18 @@ import (
...
@@ -17,18 +17,18 @@ import (
func
TestServer_PublishAndPoll
(
t
*
testing
.
T
)
{
func
TestServer_PublishAndPoll
(
t
*
testing
.
T
)
{
s
:=
newTestServer
(
t
,
newTestConfig
(
t
))
s
:=
newTestServer
(
t
,
newTestConfig
(
t
))
response1
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my first message"
)
response1
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my first message"
,
nil
)
msg1
:=
toMessage
(
t
,
response1
.
Body
.
String
())
msg1
:=
toMessage
(
t
,
response1
.
Body
.
String
())
assert
.
NotEmpty
(
t
,
msg1
.
ID
)
assert
.
NotEmpty
(
t
,
msg1
.
ID
)
assert
.
Equal
(
t
,
"my first message"
,
msg1
.
Message
)
assert
.
Equal
(
t
,
"my first message"
,
msg1
.
Message
)
response2
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my second message"
)
response2
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my second message"
,
nil
)
msg2
:=
toMessage
(
t
,
response2
.
Body
.
String
())
msg2
:=
toMessage
(
t
,
response2
.
Body
.
String
())
assert
.
NotEqual
(
t
,
msg1
.
ID
,
msg2
.
ID
)
assert
.
NotEqual
(
t
,
msg1
.
ID
,
msg2
.
ID
)
assert
.
NotEmpty
(
t
,
msg2
.
ID
)
assert
.
NotEmpty
(
t
,
msg2
.
ID
)
assert
.
Equal
(
t
,
"my second message"
,
msg2
.
Message
)
assert
.
Equal
(
t
,
"my second message"
,
msg2
.
Message
)
response
:=
request
(
t
,
s
,
"GET"
,
"/mytopic/json?poll=1"
,
""
)
response
:=
request
(
t
,
s
,
"GET"
,
"/mytopic/json?poll=1"
,
""
,
nil
)
messages
:=
toMessages
(
t
,
response
.
Body
.
String
())
messages
:=
toMessages
(
t
,
response
.
Body
.
String
())
assert
.
Equal
(
t
,
2
,
len
(
messages
))
assert
.
Equal
(
t
,
2
,
len
(
messages
))
assert
.
Equal
(
t
,
"my first message"
,
messages
[
0
]
.
Message
)
assert
.
Equal
(
t
,
"my first message"
,
messages
[
0
]
.
Message
)
...
@@ -73,6 +73,42 @@ func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
...
@@ -73,6 +73,42 @@ func TestServer_SubscribeOpenAndKeepalive(t *testing.T) {
assert
.
Nil
(
t
,
messages
[
1
]
.
Tags
)
assert
.
Nil
(
t
,
messages
[
1
]
.
Tags
)
}
}
func
TestServer_PublishAndSubscribe
(
t
*
testing
.
T
)
{
s
:=
newTestServer
(
t
,
newTestConfig
(
t
))
subscribeRR
:=
httptest
.
NewRecorder
()
subscribeCancel
:=
subscribe
(
t
,
s
,
"/mytopic/json"
,
subscribeRR
)
publishFirstRR
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my first message"
,
nil
)
assert
.
Equal
(
t
,
200
,
publishFirstRR
.
Code
)
publishSecondRR
:=
request
(
t
,
s
,
"PUT"
,
"/mytopic"
,
"my other message"
,
map
[
string
]
string
{
"Title"
:
" This is a title "
,
"X-Tags"
:
"tag1,tag 2, tag3"
,
"p"
:
"1"
,
})
assert
.
Equal
(
t
,
200
,
publishSecondRR
.
Code
)
subscribeCancel
()
messages
:=
toMessages
(
t
,
subscribeRR
.
Body
.
String
())
assert
.
Equal
(
t
,
3
,
len
(
messages
))
assert
.
Equal
(
t
,
openEvent
,
messages
[
0
]
.
Event
)
assert
.
Equal
(
t
,
messageEvent
,
messages
[
1
]
.
Event
)
assert
.
Equal
(
t
,
"mytopic"
,
messages
[
1
]
.
Topic
)
assert
.
Equal
(
t
,
"my first message"
,
messages
[
1
]
.
Message
)
assert
.
Equal
(
t
,
""
,
messages
[
1
]
.
Title
)
assert
.
Equal
(
t
,
0
,
messages
[
1
]
.
Priority
)
assert
.
Nil
(
t
,
messages
[
1
]
.
Tags
)
assert
.
Equal
(
t
,
messageEvent
,
messages
[
2
]
.
Event
)
assert
.
Equal
(
t
,
"mytopic"
,
messages
[
2
]
.
Topic
)
assert
.
Equal
(
t
,
"my other message"
,
messages
[
2
]
.
Message
)
assert
.
Equal
(
t
,
"This is a title"
,
messages
[
2
]
.
Title
)
assert
.
Equal
(
t
,
1
,
messages
[
2
]
.
Priority
)
assert
.
Equal
(
t
,
[]
string
{
"tag1"
,
"tag 2"
,
"tag3"
},
messages
[
2
]
.
Tags
)
}
func
newTestConfig
(
t
*
testing
.
T
)
*
config
.
Config
{
func
newTestConfig
(
t
*
testing
.
T
)
*
config
.
Config
{
conf
:=
config
.
New
(
":80"
)
conf
:=
config
.
New
(
":80"
)
conf
.
CacheFile
=
filepath
.
Join
(
t
.
TempDir
(),
"cache.db"
)
conf
.
CacheFile
=
filepath
.
Join
(
t
.
TempDir
(),
"cache.db"
)
...
@@ -87,16 +123,41 @@ func newTestServer(t *testing.T, config *config.Config) *Server {
...
@@ -87,16 +123,41 @@ func newTestServer(t *testing.T, config *config.Config) *Server {
return
server
return
server
}
}
func
request
(
t
*
testing
.
T
,
s
*
Server
,
method
,
url
,
body
string
)
*
httptest
.
ResponseRecorder
{
func
request
(
t
*
testing
.
T
,
s
*
Server
,
method
,
url
,
body
string
,
headers
map
[
string
]
string
)
*
httptest
.
ResponseRecorder
{
rr
:=
httptest
.
NewRecorder
()
rr
:=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
strings
.
NewReader
(
body
))
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
strings
.
NewReader
(
body
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
if
headers
!=
nil
{
for
k
,
v
:=
range
headers
{
req
.
Header
.
Set
(
k
,
v
)
}
}
s
.
handle
(
rr
,
req
)
s
.
handle
(
rr
,
req
)
return
rr
return
rr
}
}
func
subscribe
(
t
*
testing
.
T
,
s
*
Server
,
url
string
,
rr
*
httptest
.
ResponseRecorder
)
context
.
CancelFunc
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
"GET"
,
url
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
done
:=
make
(
chan
bool
)
go
func
()
{
s
.
handle
(
rr
,
req
)
done
<-
true
}()
cancelAndWaitForDone
:=
func
()
{
time
.
Sleep
(
100
*
time
.
Millisecond
)
cancel
()
<-
done
}
time
.
Sleep
(
100
*
time
.
Millisecond
)
return
cancelAndWaitForDone
}
func
toMessages
(
t
*
testing
.
T
,
s
string
)
[]
*
message
{
func
toMessages
(
t
*
testing
.
T
,
s
string
)
[]
*
message
{
messages
:=
make
([]
*
message
,
0
)
messages
:=
make
([]
*
message
,
0
)
scanner
:=
bufio
.
NewScanner
(
strings
.
NewReader
(
s
))
scanner
:=
bufio
.
NewScanner
(
strings
.
NewReader
(
s
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment