net/http/client_test.go

40 lines
967 B
Go
Raw Permalink Normal View History

package http
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_WriteRequest(t *testing.T) {
b := &strings.Builder{}
WriteRequest(b, "GET", "example.ru", "/", "HTTP/1.0", nil)
assert.Equal(t, "GET / HTTP/1.0\r\n\r\n", b.String())
}
func Test_ReadResponse(t *testing.T) {
b := bytes.NewBuffer(
[]byte("HTTP/1.1 200 OK\r\nDate: Sat, 03 Feb 2024 16:40:29 GMT\r\nContent-Length: 12\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nHello World!"),
)
r, err := ReadResponse(b)
assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode)
assert.Equal(t, "Date", r.Headers[0].Name)
assert.Equal(t, "Sat, 03 Feb 2024 16:40:29 GMT", r.Headers[0].Value)
assert.Equal(t, "Content-Length", r.Headers[1].Name)
assert.Equal(t, "12", r.Headers[1].Value)
assert.Equal(t, "Content-Type", r.Headers[2].Name)
assert.Equal(t, "text/plain; charset=utf-8", r.Headers[2].Value)
assert.Equal(t, []byte("Hello World!"), r.Body)
}