mirror of
https://github.com/cupcakearmy/codecrafters-http-server-go.git
synced 2024-12-22 08:06:27 +00:00
return content encoding
This commit is contained in:
parent
4669b2602c
commit
a0c57541c2
@ -64,6 +64,10 @@ type Routes struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Respond(conn net.Conn, req Request, res Response) {
|
func Respond(conn net.Conn, req Request, res Response) {
|
||||||
|
if res.Headers == nil {
|
||||||
|
res.Headers = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintf(conn, "%s %d %s%s", res.Version, res.Code.Code, res.Code.Message, HTTPDelimiter)
|
fmt.Fprintf(conn, "%s %d %s%s", res.Version, res.Code.Code, res.Code.Message, HTTPDelimiter)
|
||||||
bodySize := 0
|
bodySize := 0
|
||||||
if res.Body != "" {
|
if res.Body != "" {
|
||||||
@ -78,6 +82,10 @@ func Respond(conn net.Conn, req Request, res Response) {
|
|||||||
fmt.Fprintf(conn, "%s: %s%s", header, value, HTTPDelimiter)
|
fmt.Fprintf(conn, "%s: %s%s", header, value, HTTPDelimiter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Headers["Accept-Encoding"] == "gzip" {
|
||||||
|
res.Headers["Content-Encoding"] = "gzip"
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprint(conn, HTTPDelimiter)
|
fmt.Fprint(conn, HTTPDelimiter)
|
||||||
if bodySize > 0 {
|
if bodySize > 0 {
|
||||||
if res.Body != "" {
|
if res.Body != "" {
|
||||||
|
@ -38,7 +38,9 @@ func checkResponse(t *testing.T, res *http.Response, expected Expected) {
|
|||||||
t.Errorf(`Expected body to be "%s" but got "%s"`, expected.body, body)
|
t.Errorf(`Expected body to be "%s" but got "%s"`, expected.body, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fmt.Println(expected.headers)
|
||||||
for header, value := range expected.headers {
|
for header, value := range expected.headers {
|
||||||
|
// fmt.Println(header, res.Header[header])
|
||||||
if actual := res.Header[header][0]; actual != value {
|
if actual := res.Header[header][0]; actual != value {
|
||||||
t.Errorf(`Expected "%s" header to be "%s" but got "%s"`, header, value, actual)
|
t.Errorf(`Expected "%s" header to be "%s" but got "%s"`, header, value, actual)
|
||||||
}
|
}
|
||||||
@ -64,6 +66,20 @@ func TestEcho(t *testing.T) {
|
|||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func TestEchoGzip(t *testing.T) {
|
||||||
|
// input := "abc"
|
||||||
|
// req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:4221/echo/%s", input), nil)
|
||||||
|
// req.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
// client := &http.Client{}
|
||||||
|
// res, _ := client.Do(req)
|
||||||
|
|
||||||
|
// checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
||||||
|
// // "Content-Length": strconv.Itoa(len(input)),
|
||||||
|
// "Content-Type": "text/plain",
|
||||||
|
// "Content-Encoding": "gzip",
|
||||||
|
// }})
|
||||||
|
// }
|
||||||
|
|
||||||
func TestUserAgent(t *testing.T) {
|
func TestUserAgent(t *testing.T) {
|
||||||
input := "CodeCrafters/1.0"
|
input := "CodeCrafters/1.0"
|
||||||
req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil)
|
req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil)
|
||||||
|
Loading…
Reference in New Issue
Block a user