forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_http_test.go
151 lines (122 loc) · 3.54 KB
/
source_http_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
const fixtureImage = "fixtures/large.jpg"
func TestHttpImageSource(t *testing.T) {
var body []byte
var err error
buf, _ := ioutil.ReadFile(fixtureImage)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(buf)
}))
defer ts.Close()
source := NewHttpImageSource(&SourceConfig{})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err = source.GetImage(r)
if err != nil {
t.Fatalf("Error while reading the body: %s", err)
}
w.Write(body)
}
r, _ := http.NewRequest("GET", "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
if len(body) != len(buf) {
t.Error("Invalid response body")
}
}
func TestHttpImageSourceAllowedOrigin(t *testing.T) {
buf, _ := ioutil.ReadFile(fixtureImage)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(buf)
}))
defer ts.Close()
origin, _ := url.Parse(ts.URL)
origins := []*url.URL{origin}
source := NewHttpImageSource(&SourceConfig{AllowedOrigings: origins})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err := source.GetImage(r)
if err != nil {
t.Fatalf("Error while reading the body: %s", err)
}
w.Write(body)
if len(body) != len(buf) {
t.Error("Invalid response body length")
}
}
r, _ := http.NewRequest("GET", "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestHttpImageSourceNotAllowedOrigin(t *testing.T) {
origin, _ := url.Parse("http://foo")
origins := []*url.URL{origin}
source := NewHttpImageSource(&SourceConfig{AllowedOrigings: origins})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
_, err := source.GetImage(r)
if err == nil {
t.Fatal("Error cannot be empty")
}
if err.Error() != "Not allowed remote URL origin: bar.com" {
t.Fatalf("Invalid error message: %s", err)
}
}
r, _ := http.NewRequest("GET", "http://foo/bar?url=http://bar.com", nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}
func TestHttpImageSourceForwardAuthHeader(t *testing.T) {
cases := []string{
"X-Forward-Authorization",
"Authorization",
}
for _, header := range cases {
r, _ := http.NewRequest("GET", "http://foo/bar?url=http://bar.com", nil)
r.Header.Set(header, "foobar")
source := &HttpImageSource{&SourceConfig{AuthForwarding: true}}
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
oreq := &http.Request{Header: make(http.Header)}
source.setAuthorizationHeader(oreq, r)
if oreq.Header.Get("Authorization") != "foobar" {
t.Fatal("Missmatch Authorization header")
}
}
}
func TestHttpImageSourceError(t *testing.T) {
var body []byte
var err error
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("Not found"))
}))
defer ts.Close()
source := NewHttpImageSource(&SourceConfig{})
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
if !source.Matches(r) {
t.Fatal("Cannot match the request")
}
body, err = source.GetImage(r)
if err == nil {
t.Fatalf("Server response should not be valid: %s", err)
}
}
r, _ := http.NewRequest("GET", "http://foo/bar?url="+ts.URL, nil)
w := httptest.NewRecorder()
fakeHandler(w, r)
}