From 1ae6dfd220ab3153d58308984de04c5e2a40b252 Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Sun, 26 May 2024 02:43:27 +0200 Subject: [PATCH] cleanup tests --- app/server_test.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/app/server_test.go b/app/server_test.go index aacdc04..b22e462 100644 --- a/app/server_test.go +++ b/app/server_test.go @@ -13,6 +13,10 @@ import ( "testing" ) +var ( + client = &http.Client{Transport: &http.Transport{DisableCompression: true}} +) + func getBody(body io.ReadCloser) []byte { defer body.Close() bodyBytes, err := io.ReadAll(body) @@ -60,7 +64,8 @@ func TestNotFound(t *testing.T) { func TestEcho(t *testing.T) { input := "abc" - res, _ := http.Get(fmt.Sprintf("http://localhost:4221/echo/%s", input)) + req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:4221/echo/%s", input), nil) + 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", @@ -71,11 +76,11 @@ 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": "27", // Size of gzip "abc" + asGzip := string(gzipCompress([]byte(input))) + checkResponse(t, res, Expected{status: 200, body: asGzip, headers: map[string]string{ + "Content-Length": strconv.Itoa(len(asGzip)), "Content-Type": "text/plain", "Content-Encoding": "gzip", }}) @@ -84,19 +89,16 @@ func TestEchoGzip(t *testing.T) { func TestUserAgent(t *testing.T) { input := "CodeCrafters/1.0" req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil) - req.Header.Del("") req.Header.Set("User-Agent", input) - 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-Length": strconv.Itoa(len(input)), + "Content-Type": "text/plain", }}) } func TestUserAgentNoHeader(t *testing.T) { req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil) req.Header.Set("User-Agent", "") - client := &http.Client{} res, _ := client.Do(req) checkResponse(t, res, Expected{status: 400}) } @@ -108,7 +110,8 @@ func TestReadFile(t *testing.T) { os.WriteFile(tmp.Name(), []byte(input), 0755) DIR = path.Dir(tmp.Name()) - res, _ := http.Get(fmt.Sprintf("http://localhost:4221/files/%s", path.Base(tmp.Name()))) + req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:4221/files/%s", path.Base(tmp.Name())), nil) + res, _ := client.Do(req) checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{ "Content-Type": "application/octet-stream", "Content-Length": strconv.Itoa(len(input)), @@ -121,7 +124,9 @@ func TestWriteFile(t *testing.T) { defer os.Remove(tmp.Name()) DIR = path.Dir(tmp.Name()) - res, _ := http.Post(fmt.Sprintf("http://localhost:4221/files/%s", path.Base(tmp.Name())), "application/octet-stream", strings.NewReader(input)) + req, _ := http.NewRequest("POST", fmt.Sprintf("http://localhost:4221/files/%s", path.Base(tmp.Name())), strings.NewReader(input)) + req.Header.Set("Content-Type", "application/octet-stream") + res, _ := client.Do(req) checkResponse(t, res, Expected{status: 201}) contents, _ := os.ReadFile(tmp.Name())