-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineChuchNorrisAPI_test.go
50 lines (44 loc) · 1.49 KB
/
OnlineChuchNorrisAPI_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"bytes"
"encoding/json"
"github.com/heroku/chuch/go-getting-started/handlers"
"net/http"
"testing"
)
// Test_getChuckNorrisJoke online test,
// returns correct result, but reports 0 % test courage
func Test_getChuckNorrisJoke(t *testing.T) {
expextedStatusCode := http.StatusOK
//
webSite := "https://warm-meadow-53471.herokuapp.com/dialogflow"
contentType := "application/json"
// make a struct with test values
para := handlers.Para{B: "joke"}
querry := handlers.Querry{para}
strc := handlers.DialogflowPostStruct{ResponseId: "testid", FulfillmentText: "joke", QueryResult: querry}
// make json string in correct format( imitating a request from dialogflow chat-bot)
str, err := json.Marshal(strc)
if err != nil {
t.Error("failed to marshal string to json")
}
// post to online API
res, err2 := http.Post(webSite, contentType, bytes.NewBuffer(str))
if err2 != nil {
t.Error("failed to post to /dialogflow")
}
// check http status code
if res.StatusCode != expextedStatusCode {
t.Error("incorrect status code form heroku post", res.StatusCode)
}
var responseStruct handlers.DialogFlowResponceStruct
// unmarshal response form online API
err3 := json.NewDecoder(res.Body).Decode(&responseStruct)
if err3 != nil {
t.Error("unable do unmarshal dialogflow type json message", err3)
}
// check if response contains some values
if responseStruct.FulfillmentText == "" || responseStruct.Payload.Slack.Text == "" {
t.Error("received no content form API")
}
}