forked from gobuffalo/httptest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
71 lines (60 loc) · 1.59 KB
/
xml.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
package httptest
import (
"bytes"
"encoding/xml"
"net/http"
"net/http/httptest"
"github.com/gobuffalo/httptest/internal/takeon/github.com/markbates/hmax"
)
type XML struct {
URL string
handler *Handler
Headers map[string]string
Username string
Password string
}
type XMLResponse struct {
*Response
}
func (r *XMLResponse) Bind(x interface{}) {
xml.NewDecoder(r.Body).Decode(&x)
}
func (r *XML) Get() *XMLResponse {
req, _ := http.NewRequest("GET", r.URL, nil)
return r.perform(req)
}
func (r *XML) Delete() *XMLResponse {
req, _ := http.NewRequest("DELETE", r.URL, nil)
return r.perform(req)
}
func (r *XML) Post(body interface{}) *XMLResponse {
b, _ := xml.Marshal(body)
req, _ := http.NewRequest("POST", r.URL, bytes.NewReader(b))
return r.perform(req)
}
func (r *XML) Put(body interface{}) *XMLResponse {
b, _ := xml.Marshal(body)
req, _ := http.NewRequest("PUT", r.URL, bytes.NewReader(b))
return r.perform(req)
}
func (r *XML) Patch(body interface{}) *XMLResponse {
b, _ := xml.Marshal(body)
req, _ := http.NewRequest("PATCH", r.URL, bytes.NewReader(b))
return r.perform(req)
}
func (r *XML) perform(req *http.Request) *XMLResponse {
if r.handler.HmaxSecret != "" {
hmax.SignRequest(req, []byte(r.handler.HmaxSecret))
}
if r.Username != "" || r.Password != "" {
req.SetBasicAuth(r.Username, r.Password)
}
res := &XMLResponse{&Response{httptest.NewRecorder()}}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
req.Header.Set("Cookie", r.handler.Cookies)
r.handler.ServeHTTP(res, req)
r.handler.Cookies = res.Header().Get("Set-Cookie")
return res
}