/
cndb_test.go
62 lines (53 loc) · 1.29 KB
/
cndb_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
51
52
53
54
55
56
57
58
59
60
61
62
package chucknorris
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetRandomJoke(t *testing.T) {
tests := []struct {
req *CNDBRequest
jokeID int
jokeText string
}{
{nil, 1, "Joke"},
}
for i, tt := range tests {
mockResp := fmt.Sprintf(
`{ "type": "success", "value": { "id": %d, "joke": "%s", "categories": [] } }`,
tt.jokeID,
tt.jokeText,
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, mockResp)
}))
baseURL = ts.URL
joke, err := GetRandomJoke(nil)
if err != nil {
t.Fatal(err)
}
if joke.ID != tt.jokeID {
t.Errorf("Test - %d, Expected - %s , Got - %s", i, tt.jokeID, joke.ID)
continue
}
if joke.ID != tt.jokeID || joke.String() != tt.jokeText {
t.Errorf("Test - %d, Expected - %s , Got - %s", i, tt.jokeText, joke)
continue
}
}
}
func TestGetJokeByID(t *testing.T) {
mockResp := `{ "type": "success", "value": { "id": 1, "joke": "test", "categories": [] } }`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, mockResp)
}))
baseURL = ts.URL
joke, err := GetJokeByID(1, nil)
if err != nil {
t.Fatal(err)
}
if joke.String() != "test" {
t.Errorf("Expected - test , Got - %s", joke)
}
}