-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
78 lines (67 loc) · 1.69 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
package xhttp
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
func TestResponse_Body(t *testing.T) {
responseBody := "Hello, world!"
resp := &Response{
RawResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(responseBody)),
},
}
body, err := resp.Body()
assert.NoError(t, err)
assert.Equal(t, body, []byte(responseBody))
}
func TestResponse_String(t *testing.T) {
responseBody := "Hello, world!"
resp := &Response{
RawResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(responseBody)),
},
}
body, err := resp.String()
assert.NoError(t, err)
assert.Equal(t, body, responseBody)
}
func TestResponse_Json(t *testing.T) {
// 创建一个模拟的 JSON 响应
type TestData struct {
Message string `json:"message"`
}
expectedData := TestData{Message: "Hello, world!"}
jsonBody, _ := json.Marshal(expectedData)
resp := &Response{
RawResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBuffer(jsonBody)),
},
}
// 调用 Json 方法
var actualData TestData
err := resp.Json(&actualData)
assert.NoError(t, err)
assert.Equal(t, expectedData, actualData)
}
func TestResponse_Map(t *testing.T) {
// 创建一个模拟的 JSON 响应
testData := map[string]string{"message": "hello world"}
jsonBody, _ := json.Marshal(testData)
resp := &Response{
RawResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBuffer(jsonBody)),
},
}
// 调用 Json 方法
actualData, err := resp.Map()
assert.NoError(t, err)
assert.Equal(t, testData, actualData)
}