-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_core_test.go
91 lines (71 loc) · 2.13 KB
/
client_core_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package gohttp
import (
"net/http"
"testing"
)
func TestGetRequestHeader(t *testing.T) {
client := httpClient{
builder: &clientBuilder{},
}
commonHeaders := make(http.Header)
commonHeaders.Set("content-type", "application/json")
commonHeaders.Set("user-agent", "test-agent")
client.builder.headers = commonHeaders
requestHeaders := make(http.Header)
requestHeaders.Set("x-request-id", "ABC-123")
finalHeaders := client.getRequestHeaders(requestHeaders)
if len(finalHeaders) != 3 {
t.Error("we expect 3 headers")
}
if finalHeaders.Get("x-request-id") != "ABC-123" {
t.Error("we expect x-request-id to be ABC-123")
}
if finalHeaders.Get("user-agent") != "test-agent" {
t.Error("we expect user-agent to be test-agent")
}
if finalHeaders.Get("content-type") != "application/json" {
t.Error("we expect content-type to be application/json")
}
}
func TestGetRequestBody(t *testing.T) {
client := httpClient{}
t.Run("noBodyNilResponse", func(t *testing.T) {
body, err := client.getRequestBody("", nil)
if err != nil {
t.Error("no error expected when passing nil body")
}
if body != nil {
t.Error("no body expected when passing nil body")
}
})
t.Run("BodyWithJSON", func(t *testing.T) {
requestBody := []string{"one", "two"}
body, err := client.getRequestBody("application/json", requestBody)
if err != nil {
t.Error("no error expected when marshalling slice as json")
}
if string(body) != `["one","two"]` {
t.Error("invalid json obtained")
}
})
t.Run("BodyWithXML", func(t *testing.T) {
requestBody := []string{"one", "two"}
body, err := client.getRequestBody("application/xml", requestBody)
if err != nil {
t.Error("no error expected when marshalling slice as json")
}
if string(body) != `<string>one</string><string>two</string>` {
t.Error("invalid json obtained")
}
})
t.Run("BodyWithDefault", func(t *testing.T) {
requestBody := []string{"one", "two"}
body, err := client.getRequestBody("", requestBody)
if err != nil {
t.Error("no error expected when marshalling slice as json")
}
if string(body) != `["one","two"]` {
t.Error("invalid json obtained")
}
})
}