4
4
"encoding/base64"
5
5
"math/rand"
6
6
"net/http"
7
+ "strings"
7
8
8
9
. "github.com/onsi/ginkgo"
9
10
. "github.com/onsi/gomega"
@@ -49,6 +50,50 @@ var _ = Describe("ResponseWriter tests", func() {
49
50
})
50
51
})
51
52
53
+ Context ("Automatically set response content type" , func () {
54
+ xmlBodyContent := "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
55
+ htmlBodyContent := " <!DOCTYPE html><html><head><meta charset=\" UTF-8\" ><title>Title of the document</title></head><body>Content of the document......</body></html>"
56
+ It ("Does not set the content type if it's already set" , func () {
57
+ resp := NewProxyResponseWriter ()
58
+ resp .Header ().Add ("Content-Type" , "application/json" )
59
+
60
+ resp .Write ([]byte (xmlBodyContent ))
61
+
62
+ Expect ("application/json" ).To (Equal (resp .Header ().Get ("Content-Type" )))
63
+ proxyResp , err := resp .GetProxyResponse ()
64
+ Expect (err ).To (BeNil ())
65
+ Expect (1 ).To (Equal (len (proxyResp .Headers )))
66
+ Expect ("application/json" ).To (Equal (proxyResp .Headers ["Content-Type" ]))
67
+ Expect (xmlBodyContent ).To (Equal (proxyResp .Body ))
68
+ })
69
+
70
+ It ("Sets the conte type to text/xml given the body" , func () {
71
+ resp := NewProxyResponseWriter ()
72
+ resp .Write ([]byte (xmlBodyContent ))
73
+
74
+ Expect ("" ).ToNot (Equal (resp .Header ().Get ("Content-Type" )))
75
+ Expect (true ).To (Equal (strings .HasPrefix (resp .Header ().Get ("Content-Type" ), "text/xml;" )))
76
+ proxyResp , err := resp .GetProxyResponse ()
77
+ Expect (err ).To (BeNil ())
78
+ Expect (1 ).To (Equal (len (proxyResp .Headers )))
79
+ Expect (true ).To (Equal (strings .HasPrefix (proxyResp .Headers ["Content-Type" ], "text/xml;" )))
80
+ Expect (xmlBodyContent ).To (Equal (proxyResp .Body ))
81
+ })
82
+
83
+ It ("Sets the conte type to text/html given the body" , func () {
84
+ resp := NewProxyResponseWriter ()
85
+ resp .Write ([]byte (htmlBodyContent ))
86
+
87
+ Expect ("" ).ToNot (Equal (resp .Header ().Get ("Content-Type" )))
88
+ Expect (true ).To (Equal (strings .HasPrefix (resp .Header ().Get ("Content-Type" ), "text/html;" )))
89
+ proxyResp , err := resp .GetProxyResponse ()
90
+ Expect (err ).To (BeNil ())
91
+ Expect (1 ).To (Equal (len (proxyResp .Headers )))
92
+ Expect (true ).To (Equal (strings .HasPrefix (proxyResp .Headers ["Content-Type" ], "text/html;" )))
93
+ Expect (htmlBodyContent ).To (Equal (proxyResp .Body ))
94
+ })
95
+ })
96
+
52
97
Context ("Export API Gateway proxy response" , func () {
53
98
emtpyResponse := NewProxyResponseWriter ()
54
99
emtpyResponse .Header ().Add ("Content-Type" , "application/json" )
@@ -70,7 +115,7 @@ var _ = Describe("ResponseWriter tests", func() {
70
115
Expect ("hello" ).To (Equal (proxyResponse .Body ))
71
116
Expect (http .StatusOK ).To (Equal (proxyResponse .StatusCode ))
72
117
Expect (1 ).To (Equal (len (proxyResponse .Headers )))
73
- Expect ("text/plain" ).To (Equal (proxyResponse .Headers ["Content-Type" ]))
118
+ Expect (true ).To (Equal (strings . HasPrefix ( proxyResponse .Headers ["Content-Type" ], "text/plain" ) ))
74
119
Expect (proxyResponse .IsBase64Encoded ).To (BeFalse ())
75
120
})
76
121
0 commit comments