-
Notifications
You must be signed in to change notification settings - Fork 1
/
petition_test.go
executable file
·167 lines (151 loc) · 5.42 KB
/
petition_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// petition_test.go
package gridas
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"testing"
)
// TODO: Refactor!! DRY && use gocheck && ...
var body = []byte{1, 2, 3, 0, 255, 254}
const targetHost = "veryfarawayhost:1234"
const targetScheme = "https"
const rushURL = "http://rushhost:9876/path/subpath?q=a&r=b&r=c&españa=olé"
//We want to try lowercase. Don`t use package constants!!
const (
relayerHostField = "x-relayer-host"
relayerSchemeField = "x-relayer-protocol"
relayerTraceidField = "x-relayer-traceid"
relayerTopicField = "x-relayer-topic"
relayerRetryField = "x-relayer-retry"
relayerProxyField = "x-relayer-proxy"
)
func TestPetition(t *testing.T) {
for _, traceHdrToTry := range []string{relayerTraceidField, relayerTopicField} {
for _, traceid := range []string{"", "un identificador algo largo pero interesante", "*", "españa y olé"} {
original, err := http.NewRequest("GET", rushURL, bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
original.Header.Set(relayerHostField, targetHost)
original.Header.Set(relayerSchemeField, targetScheme)
original.Header.Set(traceHdrToTry, traceid)
original.Header.Set("x-another-thing", "verywell")
original.Header.Add("x-another-thing", "fandango")
petition, err := newPetition(original)
t.Logf("%#v\n", petition)
if err != nil {
t.Fatal(err)
}
if petition.TraceID != traceid {
t.Errorf("traceID %q should be equal to original %q", petition.TraceID, traceid)
}
retrieved, err := petition.Request()
if err != nil {
t.Fatal(err)
}
retrievedBody, err := ioutil.ReadAll(retrieved.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(body, retrievedBody) {
t.Error("retrieved body should be equal to sent body")
}
for _, hf := range []string{relayerHostField, relayerSchemeField, traceHdrToTry} {
if value, ok := retrieved.Header[http.CanonicalHeaderKey(hf)]; ok {
t.Errorf("retrieved header %q should have been erased, value %q", hf, value)
}
}
if !reflect.DeepEqual(retrieved.Header, original.Header) {
t.Error("retrieved header should be equal to sent header")
}
if retrieved.URL.Host != targetHost {
t.Errorf("retrieved host %q should be equal to target host %q", retrieved.URL.Host, targetHost)
}
if retrieved.URL.Scheme != targetScheme {
t.Errorf("retrieved scheme %q should be equal to target scheme %q", retrieved.URL.Scheme, targetScheme)
}
if retrieved.URL.RequestURI() != original.URL.RequestURI() {
t.Errorf("retrieved requestURI %q should be equal to original requestURI %q",
retrieved.URL.RequestURI(), original.URL.RequestURI())
}
}
}
}
func TestPetitionOlderHost(t *testing.T) {
original, err := http.NewRequest("GET", rushURL, bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
for _, u := range []url.URL{{Scheme: "http", Host: "localhost:9812"}, {Scheme: "https", Host: "another"}} {
str := u.String()
t.Log("trying older x-relayer-host", str)
original.Header.Set(relayerHostField, str)
original.Header.Set("x-another-thing", "blondie")
original.Header.Add("x-another-thing", "onewayoranother")
petition, err := newPetition(original)
t.Logf("%#v\n", petition)
if err != nil {
t.Fatal(err)
}
retrieved, err := petition.Request()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(retrieved.Header, original.Header) {
t.Error("retrieved header should be equal to sent header")
}
if retrieved.URL.Host != u.Host {
t.Errorf("retrieved host %q should be equal to target host %q", retrieved.URL.Host, u.Host)
}
if retrieved.URL.Scheme != u.Scheme {
t.Errorf("retrieved scheme %q should be equal to target scheme %q", retrieved.URL.Scheme, u.Scheme)
}
if retrieved.URL.RequestURI() != original.URL.RequestURI() {
t.Errorf("retrieved requestURI %q should be equal to original requestURI %q",
retrieved.URL.RequestURI(), original.URL.RequestURI())
}
}
}
func TestPetitionRemoveOlder(t *testing.T) {
for _, hf := range []string{RelayerProxy, RelayerTopic, RelayerRetry} {
original, err := http.NewRequest("GET", rushURL, bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
original.Header.Set(relayerHostField, targetHost)
original.Header.Set(hf, "some value")
original.Header.Set("x-another-thing", "blondie")
original.Header.Set("x-another-thing", "blondie")
original.Header.Add("x-another-thing", "onewayoranother")
petition, err := newPetition(original)
t.Logf("petition %#v\n", petition)
if err != nil {
t.Fatal(err)
}
retrieved, err := petition.Request()
if err != nil {
t.Fatal(err)
}
t.Logf("retrieved %#v\n", retrieved)
t.Logf("retrieved URL %#v\n", retrieved.URL)
if !reflect.DeepEqual(retrieved.Header, original.Header) {
t.Error("retrieved header should be equal to sent header")
}
if retrieved.URL.Host != targetHost {
t.Errorf("retrieved host %q should be equal to target host %q", retrieved.URL.Host, targetHost)
}
if retrieved.URL.Scheme != "http" {
t.Errorf("retrieved scheme %q should be equal to target scheme %q", retrieved.URL.Scheme, "http")
}
if retrieved.URL.RequestURI() != original.URL.RequestURI() {
t.Errorf("retrieved requestURI %q should be equal to original requestURI %q",
retrieved.URL.RequestURI(), original.URL.RequestURI())
}
if value, ok := retrieved.Header[http.CanonicalHeaderKey(hf)]; ok {
t.Errorf("retrieved header %q should have been erased, value %q", hf, value)
}
}
}