mirror of
https://github.com/cupcakearmy/codecrafters-http-server-go.git
synced 2024-12-22 08:06:27 +00:00
cleanup tests
This commit is contained in:
parent
de8a68ac05
commit
1ae6dfd220
@ -13,6 +13,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
client = &http.Client{Transport: &http.Transport{DisableCompression: true}}
|
||||||
|
)
|
||||||
|
|
||||||
func getBody(body io.ReadCloser) []byte {
|
func getBody(body io.ReadCloser) []byte {
|
||||||
defer body.Close()
|
defer body.Close()
|
||||||
bodyBytes, err := io.ReadAll(body)
|
bodyBytes, err := io.ReadAll(body)
|
||||||
@ -60,7 +64,8 @@ func TestNotFound(t *testing.T) {
|
|||||||
|
|
||||||
func TestEcho(t *testing.T) {
|
func TestEcho(t *testing.T) {
|
||||||
input := "abc"
|
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{
|
checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
||||||
"Content-Length": strconv.Itoa(len(input)),
|
"Content-Length": strconv.Itoa(len(input)),
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
@ -71,11 +76,11 @@ func TestEchoGzip(t *testing.T) {
|
|||||||
input := "abc"
|
input := "abc"
|
||||||
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:4221/echo/%s", input), nil)
|
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:4221/echo/%s", input), nil)
|
||||||
req.Header.Set("Accept-Encoding", "gzip")
|
req.Header.Set("Accept-Encoding", "gzip")
|
||||||
client := &http.Client{}
|
|
||||||
res, _ := client.Do(req)
|
res, _ := client.Do(req)
|
||||||
|
|
||||||
checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
asGzip := string(gzipCompress([]byte(input)))
|
||||||
"Content-Length": "27", // Size of gzip "abc"
|
checkResponse(t, res, Expected{status: 200, body: asGzip, headers: map[string]string{
|
||||||
|
"Content-Length": strconv.Itoa(len(asGzip)),
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
"Content-Encoding": "gzip",
|
"Content-Encoding": "gzip",
|
||||||
}})
|
}})
|
||||||
@ -84,19 +89,16 @@ func TestEchoGzip(t *testing.T) {
|
|||||||
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)
|
||||||
req.Header.Del("")
|
|
||||||
req.Header.Set("User-Agent", input)
|
req.Header.Set("User-Agent", input)
|
||||||
client := &http.Client{}
|
|
||||||
res, _ := client.Do(req)
|
res, _ := client.Do(req)
|
||||||
checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
||||||
"Content-Length": strconv.Itoa(len(input)),
|
// "Content-Length": strconv.Itoa(len(input)),
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
func TestUserAgentNoHeader(t *testing.T) {
|
func TestUserAgentNoHeader(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil)
|
req, _ := http.NewRequest("GET", "http://localhost:4221/user-agent", nil)
|
||||||
req.Header.Set("User-Agent", "")
|
req.Header.Set("User-Agent", "")
|
||||||
client := &http.Client{}
|
|
||||||
res, _ := client.Do(req)
|
res, _ := client.Do(req)
|
||||||
checkResponse(t, res, Expected{status: 400})
|
checkResponse(t, res, Expected{status: 400})
|
||||||
}
|
}
|
||||||
@ -108,7 +110,8 @@ func TestReadFile(t *testing.T) {
|
|||||||
os.WriteFile(tmp.Name(), []byte(input), 0755)
|
os.WriteFile(tmp.Name(), []byte(input), 0755)
|
||||||
DIR = path.Dir(tmp.Name())
|
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{
|
checkResponse(t, res, Expected{status: 200, body: input, headers: map[string]string{
|
||||||
"Content-Type": "application/octet-stream",
|
"Content-Type": "application/octet-stream",
|
||||||
"Content-Length": strconv.Itoa(len(input)),
|
"Content-Length": strconv.Itoa(len(input)),
|
||||||
@ -121,7 +124,9 @@ func TestWriteFile(t *testing.T) {
|
|||||||
defer os.Remove(tmp.Name())
|
defer os.Remove(tmp.Name())
|
||||||
DIR = path.Dir(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})
|
checkResponse(t, res, Expected{status: 201})
|
||||||
|
|
||||||
contents, _ := os.ReadFile(tmp.Name())
|
contents, _ := os.ReadFile(tmp.Name())
|
||||||
|
Loading…
Reference in New Issue
Block a user