diff options
author | Thomas Balthazar <tbalthazar@users.noreply.github.com> | 2016-05-17 14:56:38 +0200 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2016-05-17 08:56:38 -0400 |
commit | 5580c28e54015b78893c33cc3bf41da75712c4cb (patch) | |
tree | bb8c8ac07de4046b6131b45e26bce819f6ab88af /model/incoming_webhook_test.go | |
parent | 45527a9c1d370a2de9584d9dbe8e44ce2c7a587e (diff) | |
download | chat-5580c28e54015b78893c33cc3bf41da75712c4cb.tar.gz chat-5580c28e54015b78893c33cc3bf41da75712c4cb.tar.bz2 chat-5580c28e54015b78893c33cc3bf41da75712c4cb.zip |
PLT-2188 Integrations: Support raw new lines in the text payload (#2993)
* Integrations: Support raw new lines in the text payload
* Improve support for raw new lines in text payload
The regexp used to escape control characters now also searches for
additional fields:
text|fallback|pretext|author_name|title|value
Diffstat (limited to 'model/incoming_webhook_test.go')
-rw-r--r-- | model/incoming_webhook_test.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/model/incoming_webhook_test.go b/model/incoming_webhook_test.go index 3f1754244..8b62bbbdf 100644 --- a/model/incoming_webhook_test.go +++ b/model/incoming_webhook_test.go @@ -100,3 +100,75 @@ func TestIncomingWebhookPreUpdate(t *testing.T) { o := IncomingWebhook{} o.PreUpdate() } + +func TestIncomingWebhookRequestFromJson(t *testing.T) { + texts := []string{ + `this is a test`, + `this is a test + that contains a newline and tabs`, + `this is a test \"foo + that contains a newline and tabs`, + `this is a test \"foo\" + that contains a newline and tabs`, + `this is a test \"foo\" + \" that contains a newline and tabs`, + `this is a test \"foo\" + + \" that contains a newline and tabs + `, + } + + for i, text := range texts { + // build a sample payload with the text + payload := `{ + "text": "` + text + `", + "attachments": [ + { + "fallback": "` + text + `", + + "color": "#36a64f", + + "pretext": "` + text + `", + + "author_name": "` + text + `", + "author_link": "http://flickr.com/bobby/", + "author_icon": "http://flickr.com/icons/bobby.jpg", + + "title": "` + text + `", + "title_link": "https://api.slack.com/", + + "text": "` + text + `", + + "fields": [ + { + "title": "` + text + `", + "value": "` + text + `", + "short": false + } + ], + + "image_url": "http://my-website.com/path/to/image.jpg", + "thumb_url": "http://example.com/path/to/thumb.png" + } + ] + }` + + // try to create an IncomingWebhookRequest from the payload + data := strings.NewReader(payload) + iwr := IncomingWebhookRequestFromJson(data) + + // After it has been decoded, the JSON string won't contain the escape char anymore + expected := strings.Replace(text, `\"`, `"`, -1) + if iwr == nil { + t.Fatal("IncomingWebhookRequest should not be nil") + } + if iwr.Text != expected { + t.Fatalf("Sample %d text should be: %s, got: %s", i, expected, iwr.Text) + } + attachments := iwr.Attachments.([]interface{}) + attachment := attachments[0].(map[string]interface{}) + if attachment["text"] != expected { + t.Fatalf("Sample %d attachment text should be: %s, got: %s", i, expected, attachment["text"]) + } + } +} |