-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
128 lines (107 loc) · 2.93 KB
/
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
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
127
128
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
type MockTransport struct {
Response MockResponse
Error error
}
func NewMockTransport(statusCode int, headers map[string]string, body map[string][]byte) *MockTransport {
return &MockTransport{
Response: NewMockResponse(statusCode, headers, body),
}
}
// RoundTrip receives HTTP requests and routes them to the appropriate
// responder. It is required to implement the http.RoundTripper
// interface. You will not interact with this directly, instead the
// *http.Client you are using will call it for you.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if m.Error != nil {
return nil, m.Error
}
return m.Response.MakeResponse(req), nil
}
type MockResponse struct {
StatusCode int
HeadersMap map[string]string
Body map[string][]byte
}
func NewMockResponse(statusCode int, headers map[string]string, body map[string][]byte) MockResponse {
if headers == nil {
headers = map[string]string{}
}
if body == nil {
body = map[string][]byte{}
}
return MockResponse{StatusCode: statusCode, HeadersMap: headers, Body: body}
}
func (r *MockResponse) MakeResponse(req *http.Request) *http.Response {
// Get Status
status := strconv.Itoa(r.StatusCode) + " " + http.StatusText(r.StatusCode)
// Get HTTP Headers
header := http.Header{}
for name, value := range r.HeadersMap {
header.Set(name, value)
}
// Get HTTP Response
httpResponse := r.Body[req.URL.String()]
if httpResponse == nil {
// Print Empty Request
fmt.Println(req.URL.String())
// Get First Available Process
for _, response := range r.Body {
httpResponse = response
break
}
}
// Set Content Length
contentLength := len(httpResponse)
header.Set("Content-Length", strconv.Itoa(contentLength))
// Set HTTP Response
res := &http.Response{
Status: status,
StatusCode: r.StatusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(httpResponse)),
ContentLength: int64(contentLength),
TransferEncoding: []string{},
Close: false,
Uncompressed: false,
Trailer: nil,
Request: req,
TLS: nil,
}
// Should no set Content-Length header when 204 or 304
if r.StatusCode == http.StatusNoContent || r.StatusCode == http.StatusNotModified {
if res.ContentLength != 0 {
res.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
res.ContentLength = 0
}
header.Del("Content-Length")
}
return res
}
type MockDatabase struct {
Database map[string][]byte
}
func (d *MockDatabase) AddToDatabase(key string, value interface{}) {
// Convert to JSON
jsonResponse, err := json.Marshal(value)
if err != nil {
return
}
// Check Database
if d.Database == nil {
d.Database = make(map[string][]byte)
}
// Add to Database
d.Database[key] = jsonResponse
}