-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransport_test.go
75 lines (68 loc) · 1.67 KB
/
transport_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
// transport_test.go
//
// Copyright (c) 2018-2023 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package pixeldrain
import (
"net/http"
"testing"
"github.com/go-openapi/runtime"
)
type mockRoundTripper struct {
Request *http.Request
Response *http.Response
}
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
m.Request = req
return m.Response, nil
}
func TestContentTypeFixer(t *testing.T) {
cases := []struct {
name string
status int
contentType string
expect string
}{
{
name: "replace with JSON",
status: http.StatusOK,
contentType: runtime.JSONMime,
expect: runtime.JSONMime,
},
{
name: "replace with octet-stream",
status: http.StatusOK,
contentType: runtime.DefaultMime,
expect: runtime.DefaultMime,
},
{
name: "error",
status: http.StatusNotFound,
contentType: runtime.DefaultMime,
expect: "text/plain charset=utf8",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
mock := &mockRoundTripper{
Response: &http.Response{
Status: "OK",
StatusCode: c.status,
Header: make(http.Header),
},
}
mock.Response.Header.Set(runtime.HeaderContentType, "text/plain charset=utf8")
transporter := newRoundTripper(mock, c.contentType)
res, err := transporter.RoundTrip(nil)
if err != nil {
t.Fatal("RoundTrip returns an error:", err)
}
if cType := res.Header.Get(runtime.HeaderContentType); cType != c.expect {
t.Errorf("content-type is %v, expected %v", cType, c.expect)
}
})
}
}