-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_test.go
98 lines (90 loc) · 3.44 KB
/
echo_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"github.com/julienschmidt/httprouter"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Echo test", func() {
Context("Test GET ", func() {
It("should response to GET request without body with hdr application/json", func() {
req, _ := http.NewRequest("GET", "/echo", nil)
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r := createRoute()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
Expect(contentType).To(Equal("application/json"))
Expect(w.Code).To(Equal(http.StatusOK))
Expect(w.Body.String()).To(Equal(""))
})
It("should response to GET request with body and hdr application/xml", func() {
buff := bytes.NewBufferString("blahblah")
req, _ := http.NewRequest("GET", "/echo", buff)
req.Header.Add("Content-Type", "application/xml")
w := httptest.NewRecorder()
r := createRoute()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
Expect(contentType).To(Equal("application/xml"))
Expect(w.Code).To(Equal(http.StatusOK))
Expect(w.Body.String()).To(Equal("blahblah"))
})
It("should response to POST request with body and a header", func() {
buff := bytes.NewBufferString("blahblah")
req, _ := http.NewRequest("GET", "/echo?echo-hdr=X-API-AUTH", buff)
req.Header.Add("X-API-AUTH", "blahblah")
req.Header.Add("Content-Type", "application/xml")
w := httptest.NewRecorder()
r := createRoute()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
customHdr := w.Header().Get("X-API-AUTH")
Expect(contentType).To(Equal("application/xml"))
Expect(customHdr).To(Equal("blahblah"))
Expect(w.Code).To(Equal(http.StatusOK))
Expect(w.Body.String()).To(Equal("blahblah"))
})
It("should response to POST request with body and multiple headers ", func() {
buff := bytes.NewBufferString("blahblah")
req, _ := http.NewRequest("GET", "/echo?echo-hdr=X-API-AUTH,X-AAA", buff)
req.Header.Add("X-API-AUTH", "blahblah")
req.Header.Add("X-AAA", "somebody")
req.Header.Add("Content-Type", "application/xml")
w := httptest.NewRecorder()
r := createRoute()
r.ServeHTTP(w, req)
Expect("application/xml").To(Equal(w.Header().Get("Content-Type")))
Expect("blahblah").To(Equal(w.Header().Get("X-API-AUTH")))
Expect("somebody").To(Equal(w.Header().Get("X-AAA")))
Expect(http.StatusOK).To(Equal(w.Code))
Expect("blahblah").To(Equal(w.Body.String()))
})
})
Context("test POST", func() {
It("should response to POST request with body and hdr application/xml", func() {
buff := bytes.NewBufferString("blahblah")
r, _ := http.NewRequest("POST", "/echo", buff)
r.Header.Add("Content-Type", "application/xml")
w := httptest.NewRecorder()
var k []httprouter.Param
handleEcho(w, r, k)
contentType := w.Header().Get("Content-Type")
Expect(contentType).To(Equal("application/xml"))
Expect(w.Code).To(Equal(http.StatusOK))
Expect(w.Body.String()).To(Equal("blahblah"))
})
It("should response 400 when dummy-status param is 400", func() {
req, _ := http.NewRequest("POST", "/echo?dummy-status=400", nil)
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
r := createRoute()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
Expect(contentType).To(Equal("application/json"))
Expect(w.Code).To(Equal(http.StatusBadRequest))
})
})
})