-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpmockserver.go
131 lines (98 loc) · 2.66 KB
/
httpmockserver.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
129
130
131
package httpmockserver
import (
"bytes"
"encoding/base64"
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
)
// These are basic functions which exist just to show how to use a mock server for testing
const (
contentTypeJSON = "application/json;charset=utf-8"
contentTypeXML = "application/xml"
)
var ErrUpstreamFailure = errors.New("upstream failure")
var (
username string
password string
)
type FooClient struct {
client *http.Client
request *http.Request
}
type ResponseJSONRec struct {
Result string `json:"result"`
}
type ResponseXMLRec struct {
XMLName xml.Name `xml:"xml"`
Text string `xml:",chardata"`
Body struct {
Text string `xml:",chardata"`
Result string `xml:"Result"`
} `xml:"body"`
}
type RequestRec struct {
Param string `json:"param"`
}
func NewFooClient(url, username, password string) *FooClient {
var newClient FooClient
client := &http.Client{}
request, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return &FooClient{}
}
addBasicAuth(request, username, password)
newClient.client = client
newClient.request = request
return &newClient
}
func (fc *FooClient) DoStuffJSON(param string) (string, error) {
fc.request.Header.Set("Content-Type", contentTypeJSON)
// Do stuff does something and creates a request body
body := io.NopCloser(bytes.NewReader([]byte(`{"param":"` + param + `"}`)))
// Send the request
fc.request.Body = body
response, err := fc.client.Do(fc.request)
if err != nil {
return "", err
}
defer response.Body.Close()
// Only one example
if response.StatusCode == http.StatusInternalServerError {
return "", ErrUpstreamFailure
}
// Read the response
var responseBody ResponseJSONRec
decoder := json.NewDecoder(response.Body)
decoder.Decode(&responseBody)
return responseBody.Result, nil
}
func (fc *FooClient) DoStuffXML(param string) (string, error) {
fc.request.Header.Set("Content-Type", contentTypeXML)
// Do stuff does something and creates a request body
body := io.NopCloser(bytes.NewReader([]byte(`<Request>` + param + `</Request>`)))
// Send the request
fc.request.Body = body
response, err := fc.client.Do(fc.request)
if err != nil {
return "", err
}
defer response.Body.Close()
// Only one example
if response.StatusCode == http.StatusInternalServerError {
return "", ErrUpstreamFailure
}
// Read the body
var responseBody ResponseXMLRec
decoder := xml.NewDecoder(response.Body)
decoder.Decode(&responseBody)
return responseBody.Body.Result, nil
}
func addBasicAuth(r *http.Request, u, p string) {
auth := u + ":" + p
c := base64.StdEncoding.EncodeToString([]byte(auth))
b := "Basic "
r.Header.Add("Authorization", b+c)
}