mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-07-20 10:04:08 +00:00
Docs, tests
This commit is contained in:
parent
b7679c7826
commit
499b2fb0d6
5 changed files with 82 additions and 2 deletions
|
@ -1010,7 +1010,7 @@ func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, fi
|
|||
return false, false, "", "", false, errHTTPBadRequestActionsInvalid.Wrap(e.Error())
|
||||
}
|
||||
}
|
||||
contentType, markdown := readParam(r, "content-type"), readBoolParam(r, false, "x-markdown", "markdown", "md")
|
||||
contentType, markdown := readParam(r, "content-type", "content_type"), readBoolParam(r, false, "x-markdown", "markdown", "md")
|
||||
if markdown || strings.ToLower(contentType) == "text/markdown" {
|
||||
m.ContentType = "text/markdown"
|
||||
}
|
||||
|
@ -1789,6 +1789,9 @@ func (s *Server) transformBodyJSON(next handleFunc) handleFunc {
|
|||
if m.Icon != "" {
|
||||
r.Header.Set("X-Icon", m.Icon)
|
||||
}
|
||||
if m.Markdown {
|
||||
r.Header.Set("X-Markdown", "yes")
|
||||
}
|
||||
if len(m.Actions) > 0 {
|
||||
actionsStr, err := json.Marshal(m.Actions)
|
||||
if err != nil {
|
||||
|
|
|
@ -1518,6 +1518,39 @@ func TestServer_PublishActions_AndPoll(t *testing.T) {
|
|||
require.Equal(t, "target_temp_f=65", m.Actions[1].Body)
|
||||
}
|
||||
|
||||
func TestServer_PublishMarkdown(t *testing.T) {
|
||||
s := newTestServer(t, newTestConfig(t))
|
||||
response := request(t, s, "PUT", "/mytopic", "_underline this_", map[string]string{
|
||||
"Content-Type": "text/markdown",
|
||||
})
|
||||
require.Equal(t, 200, response.Code)
|
||||
|
||||
m := toMessage(t, response.Body.String())
|
||||
require.Equal(t, "_underline this_", m.Message)
|
||||
require.Equal(t, "text/markdown", m.ContentType)
|
||||
}
|
||||
|
||||
func TestServer_PublishMarkdown_QueryParam(t *testing.T) {
|
||||
s := newTestServer(t, newTestConfig(t))
|
||||
response := request(t, s, "PUT", "/mytopic?md=1", "_underline this_", nil)
|
||||
require.Equal(t, 200, response.Code)
|
||||
|
||||
m := toMessage(t, response.Body.String())
|
||||
require.Equal(t, "_underline this_", m.Message)
|
||||
require.Equal(t, "text/markdown", m.ContentType)
|
||||
}
|
||||
|
||||
func TestServer_PublishMarkdown_NotMarkdown(t *testing.T) {
|
||||
s := newTestServer(t, newTestConfig(t))
|
||||
response := request(t, s, "PUT", "/mytopic", "_underline this_", map[string]string{
|
||||
"Content-Type": "not-markdown",
|
||||
})
|
||||
require.Equal(t, 200, response.Code)
|
||||
|
||||
m := toMessage(t, response.Body.String())
|
||||
require.Equal(t, "", m.ContentType)
|
||||
}
|
||||
|
||||
func TestServer_PublishAsJSON(t *testing.T) {
|
||||
s := newTestServer(t, newTestConfig(t))
|
||||
body := `{"topic":"mytopic","message":"A message","title":"a title\nwith lines","tags":["tag1","tag 2"],` +
|
||||
|
@ -1535,12 +1568,25 @@ func TestServer_PublishAsJSON(t *testing.T) {
|
|||
require.Equal(t, "google.pdf", m.Attachment.Name)
|
||||
require.Equal(t, "http://ntfy.sh", m.Click)
|
||||
require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon)
|
||||
require.Equal(t, "", m.ContentType)
|
||||
|
||||
require.Equal(t, 4, m.Priority)
|
||||
require.True(t, m.Time > time.Now().Unix()+29*60)
|
||||
require.True(t, m.Time < time.Now().Unix()+31*60)
|
||||
}
|
||||
|
||||
func TestServer_PublishAsJSON_Markdown(t *testing.T) {
|
||||
s := newTestServer(t, newTestConfig(t))
|
||||
body := `{"topic":"mytopic","message":"**This is bold**","markdown":true}`
|
||||
response := request(t, s, "PUT", "/", body, nil)
|
||||
require.Equal(t, 200, response.Code)
|
||||
|
||||
m := toMessage(t, response.Body.String())
|
||||
require.Equal(t, "mytopic", m.Topic)
|
||||
require.Equal(t, "**This is bold**", m.Message)
|
||||
require.Equal(t, "text/markdown", m.ContentType)
|
||||
}
|
||||
|
||||
func TestServer_PublishAsJSON_RateLimit_MessageDailyLimit(t *testing.T) {
|
||||
// Publishing as JSON follows a different path. This ensures that rate
|
||||
// limiting works for this endpoint as well
|
||||
|
|
|
@ -101,6 +101,7 @@ type publishMessage struct {
|
|||
Icon string `json:"icon"`
|
||||
Actions []action `json:"actions"`
|
||||
Attach string `json:"attach"`
|
||||
Markdown bool `json:"markdown"`
|
||||
Filename string `json:"filename"`
|
||||
Email string `json:"email"`
|
||||
Call string `json:"call"`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue