requests/response_test.go

63 lines
1.3 KiB
Go
Raw Normal View History

2018-10-17 06:25:17 +00:00
package requests
2018-10-17 10:38:04 +00:00
import (
2019-09-03 19:08:28 +00:00
"net/http"
2018-10-17 10:38:04 +00:00
"testing"
2019-09-03 19:08:28 +00:00
2019-09-05 08:32:12 +00:00
"github.com/474420502/gjson"
2018-10-17 10:38:04 +00:00
)
2019-09-04 03:35:21 +00:00
func TestFromHTTPResponse(t *testing.T) {
2018-10-17 10:38:04 +00:00
2019-09-03 19:08:28 +00:00
var gresp *http.Response
var err error
gresp, err = http.DefaultClient.Get("http://httpbin.org/get")
if err != nil {
t.Error(err)
}
2019-09-04 03:35:21 +00:00
resp, err := FromHTTPResponse(gresp, false)
2019-09-03 19:08:28 +00:00
if err != nil {
t.Error(err)
}
if gjson.Get(resp.Content(), "headers.Host").String() != "httpbin.org" {
t.Error("headers.Host != httpbin.org ?")
}
if resp.GetSrcResponse().StatusCode != 200 {
t.Error("StatusCode != 200")
}
if len(resp.GetSrcResponse().Header) == 0 {
t.Error("esp.GetSrcResponse().Header == nil")
}
if resp.GetStatue() != "200 OK" || resp.GetStatueCode() != 200 {
t.Error(" resp.GetStatue() != 200 OK")
}
if len(resp.GetHeader()["Content-Length"]) != 1 {
t.Error("resp.GetHeader() is error ?")
}
if int64(len(resp.Content())) != resp.GetContentLength() {
t.Error("content len is not equal")
}
2018-10-17 10:38:04 +00:00
}
2019-09-04 03:35:21 +00:00
func TestResponseDeflate(t *testing.T) {
2019-09-04 03:35:21 +00:00
ses := NewSession()
if wf := ses.Get("http://httpbin.org/get"); wf != nil {
wf.AddHeader("accept-encoding", "deflate")
resp, err := wf.Execute()
if err != nil {
t.Error(err)
} else {
if gjson.Get(resp.Content(), "headers.Accept-Encoding").String() != "deflate" {
t.Error("Accept-Encoding != deflate ?")
}
2019-09-04 03:35:21 +00:00
}
}
}