-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
89 lines (74 loc) · 2.32 KB
/
client_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
// SPDX-FileCopyrightText: 2018-2024 caixw
//
// SPDX-License-Identifier: MIT
package web
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/mux/v9/header"
"github.com/issue9/web/internal/qheader"
"github.com/issue9/web/selector"
)
func TestClient_NewRequest(t *testing.T) {
a := assert.New(t, false)
codec := newCodec(a)
sel := selector.NewRoundRobin(false, 1)
sel.Update(selector.NewPeer("https://example.com"))
c := NewClient(nil, codec, sel, header.JSON, json.Marshal, header.XRequestID, func() string { return "123" })
a.NotNil(c).
NotNil(c.marshal).
NotNil(c.Client())
req, err := c.NewRequest(http.MethodPost, "/post", &object{Age: 11})
a.NotError(err).NotNil(req).
Equal(req.Header.Get(header.Accept), codec.acceptHeader).
Equal(req.Header.Get(header.XRequestID), "123").
Equal(req.Header.Get(header.AcceptEncoding), codec.acceptEncodingHeader).
Equal(req.Header.Get(header.ContentType), qheader.BuildContentType(header.JSON, header.UTF8))
}
func TestClient_ParseResponse(t *testing.T) {
a := assert.New(t, false)
codec := newCodec(a)
sel := selector.NewRoundRobin(false, 1)
sel.Update(selector.NewPeer("https://example.com"))
c := NewClient(nil, codec, sel, header.JSON, json.Marshal, "", nil)
a.NotNil(c).
NotNil(c.marshal)
t.Run("empty", func(*testing.T) {
resp := &http.Response{}
p := newProblem
a.NotError(c.ParseResponse(resp, nil, p))
})
t.Run("content-length=0", func(*testing.T) {
resp := &http.Response{
Header: http.Header{},
}
resp.Header.Set(header.ContentLength, "0")
a.NotError(c.ParseResponse(resp, nil, nil))
})
t.Run("normal", func(*testing.T) {
h := http.Header{}
obj := &object{Age: 11}
data, err := json.Marshal(obj)
a.NotError(err).NotNil(data)
body := bytes.NewBuffer(data)
h.Set(header.ContentLength, strconv.Itoa(body.Len()))
h.Set(header.ContentType, qheader.BuildContentType(header.JSON, header.UTF8))
h.Set(header.Accept, header.JSON)
h.Set(header.AcceptCharset, header.UTF8)
h.Set(header.AcceptEncoding, "gzip")
resp := &http.Response{
Header: h,
Body: io.NopCloser(body),
StatusCode: http.StatusOK,
ContentLength: int64(body.Len()),
}
rsp := &object{}
p := newProblem
a.NotError(c.ParseResponse(resp, rsp, p)).Equal(rsp, obj)
})
}