forked from SpectoLabs/hoverfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulation_test.go
97 lines (71 loc) · 2.44 KB
/
manipulation_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
package hoverfly
import (
"io/ioutil"
"net/http"
"testing"
)
func TestReconstructRequest(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
// changing payload so we don't have to call middleware
request := RequestDetails{
Path: "/random-path",
Method: "POST",
Query: "?foo=bar",
Destination: "changed.destination.com",
}
payload := Payload{Request: request}
c := NewConstructor(req, payload)
newRequest, err := c.ReconstructRequest()
expect(t, err, nil)
expect(t, newRequest.Method, "POST")
expect(t, newRequest.URL.Path, "/random-path")
expect(t, newRequest.Host, "changed.destination.com")
expect(t, newRequest.URL.RawQuery, "?foo=bar")
}
func TestReconstructRequestBodyPayload(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
payload := Payload{}
c := NewConstructor(req, payload)
c.payload.Request.Method = "OPTIONS"
c.payload.Request.Destination = "newdestination"
c.payload.Request.Body = "new request body here"
newRequest, err := c.ReconstructRequest()
expect(t, err, nil)
expect(t, newRequest.Method, "OPTIONS")
expect(t, newRequest.Host, "newdestination")
body, err := ioutil.ReadAll(newRequest.Body)
expect(t, err, nil)
expect(t, string(body), "new request body here")
}
func TestReconstructRequestHeadersPayload(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("Header", "ValueX")
payload := Payload{}
c := NewConstructor(req, payload)
c.payload.Request.Headers = req.Header
c.payload.Request.Destination = "destination.com"
newRequest, err := c.ReconstructRequest()
expect(t, err, nil)
expect(t, newRequest.Header.Get("Header"), "ValueX")
}
func TestReconstructResponseHeadersPayload(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
payload := Payload{}
payload.Response.Status = 201
payload.Response.Body = "body here"
headers := make(map[string][]string)
headers["Header"] = []string{"one"}
payload.Response.Headers = headers
c := NewConstructor(req, payload)
response := c.ReconstructResponse()
expect(t, response.Header.Get("Header"), headers["Header"][0])
}
func TestReconstructionFailure(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
payload := Payload{}
c := NewConstructor(req, payload)
c.payload.Request.Method = "GET"
c.payload.Request.Body = "new request body here"
_, err := c.ReconstructRequest()
refute(t, err, nil)
}