-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponse_test.go
86 lines (76 loc) · 2.36 KB
/
response_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
package room
import (
"io"
"net/http"
"net/url"
"strings"
"testing"
)
// TestNewResponse tests the NewResponse function.
func TestNewResponse(t *testing.T) {
// Mock HTTP response
httpResp := &http.Response{
StatusCode: http.StatusOK,
Request: &http.Request{
Method: http.MethodGet,
URL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/test",
},
},
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("test body")),
}
// Test case for successful creation of Response
resp := NewResponse(httpResp, httpResp.Request)
if resp.StatusCode != http.StatusOK {
t.Errorf("NewResponse() returned response with unexpected status code: %d", resp.StatusCode)
}
}
// TestResponse_OK tests the OK method of the Response struct.
func TestResponse_OK(t *testing.T) {
// Test case for successful response (status code 200)
response := Response{StatusCode: 200}
if !response.OK() {
t.Error("Response OK() returned false for status code 200")
}
// Test case for unsuccessful response (status code 404)
response = Response{StatusCode: 404}
if response.OK() {
t.Error("Response OK() returned true for status code 404")
}
}
// TestResponse_SetHeader tests the SetHeader method of the Response struct.
func TestResponse_SetHeader(t *testing.T) {
// Test case for setting header
header := http.Header{}
header.Add("Content-Type", "application/json")
response := Response{}
response = response.setHeader(header)
if response.Header == nil {
t.Error("Response SetHeader() did not set the header correctly")
}
}
// TestResponse_SetRequestHeader tests the SetRequestHeader method of the Response struct.
func TestResponse_SetRequestHeader(t *testing.T) {
// Test case for setting request header
header := http.Header{}
header.Add("Content-Type", "application/json")
response := Response{}
response = response.setRequestHeader(header)
if response.Request.Header == nil {
t.Error("Response SetRequestHeader() did not set the request header correctly")
}
}
// TestResponse_SetData tests the SetData method of the Response struct.
func TestResponse_SetData(t *testing.T) {
// Test case for setting response data
httpResp := &http.Response{
Body: io.NopCloser(strings.NewReader("test data")),
}
response := Response{}.setData(httpResp)
if len(response.Data) == 0 {
t.Error("Response SetData() did not set the response data correctly")
}
}