-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_test.go
More file actions
126 lines (110 loc) · 3.57 KB
/
http_test.go
File metadata and controls
126 lines (110 loc) · 3.57 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package sumoll
import (
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"testing"
)
const os_env_http_source = "SUMOLL_TEST_HTTP_SOURCE_URL"
var (
httpSourceURL = os.Getenv(os_env_http_source)
)
func TestHTTPSourceClientSendIntegration(t *testing.T) {
if httpSourceURL == "" {
t.Skip(os_env_http_source, "is not set. Value:", httpSourceURL)
}
t.Log(os_env_http_source, "is set. Executing integration test")
table := []struct {
body string
}{
{"hogehoge"},
}
for _, row := range table {
u, err := url.Parse(httpSourceURL)
if err != nil {
t.Fatalf("url.Parse(%s) got error: %s", httpSourceURL, err)
}
c, err := NewHTTPSourceClient(u)
if err != nil {
t.Fatalf("NewHTTPSourceClient got error")
}
err = c.Send(strings.NewReader(row.body))
if err != nil {
t.Error("Error response when sending", row.body, "to", u, ". Err:", err)
}
}
}
type httpClientMock struct {
checkCategoryValue, checkHostnameValue, checkSourcenameValue string
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error { return nil }
const urlForUnitTests = "https://localEndpoint/receiver/v1/http/myUniqueID"
func (hc httpClientMock) Do(req *http.Request) (*http.Response, error) {
operation := req.Method
requestSize := req.ContentLength
log.Printf("Mock intercepted %s request against %s with a body of %d bytes", operation, req.Host, requestSize)
responseToSend := http.StatusBadRequest
switch operation {
case "POST":
expectedURL, _ := url.Parse(urlForUnitTests)
if req.URL == nil || *req.URL != *expectedURL {
log.Println("Request against mock did not match the expected URL", expectedURL)
} else if hc.checkCategoryValue != "" && !hasHeaderWithValue(req, "X-Sumo-Category", hc.checkCategoryValue) {
log.Println("Request against mock did not have the expected category value", hc.checkCategoryValue)
} else if hc.checkHostnameValue != "" && !hasHeaderWithValue(req, "X-Sumo-Host", hc.checkHostnameValue) {
log.Println("Request against mock did not have the expected hostname value", hc.checkHostnameValue)
} else if hc.checkSourcenameValue != "" && !hasHeaderWithValue(req, "X-Sumo-Name", hc.checkSourcenameValue) {
log.Println("Request against mock did not have the expected sourcename value", hc.checkSourcenameValue)
} else {
log.Println("All mock tests were successful")
responseToSend = http.StatusOK
}
}
return &http.Response{
Body: nopCloser{},
StatusCode: responseToSend,
Status: http.StatusText(responseToSend),
}, nil
}
func hasHeaderWithValue(request *http.Request, header string, expectedValue string) bool {
return request.Header.Get(header) == expectedValue
}
func TestHTTPSourceClientSendUnit(t *testing.T) {
table := []struct {
category, hostname, sourcename string
}{
{"", "", ""},
{"testCategory", "", ""},
{"", "testHostname", ""},
{"", "", "testSourcename"},
{"testCategory", "testHostname", "testSourcename"},
}
for _, values := range table {
localUrl, _ := url.Parse(urlForUnitTests)
c, err := NewHTTPSourceClient(localUrl,
SetXSumoCategoryHeader(values.category),
SetXSumoHostHeader(values.hostname),
SetXSumoNameHeader(values.sourcename),
)
if err != nil {
t.Fatalf("NewHTTPSourceClient got error: %s", err)
}
c.client = &httpClientMock{
checkCategoryValue: values.category,
checkHostnameValue: values.hostname,
checkSourcenameValue: values.sourcename,
}
err = c.Send(strings.NewReader("hogehoge"))
if err != nil {
t.Error("Error response when sending payload to", localUrl,
"with", values.category, values.hostname, values.sourcename,
". Err:", err)
}
}
}