forked from imroc/req
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
327 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,62 @@ | ||
package req | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestDumpText(t *testing.T) { | ||
SetFlags(LstdFlags | Lcost) | ||
reqBody := "request body" | ||
respBody := "response body" | ||
reqHeader := "Request-Header" | ||
respHeader := "Response-Header" | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set(respHeader, "req") | ||
w.Write([]byte(respBody)) | ||
} | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
header := Header{ | ||
reqHeader: "hello", | ||
} | ||
resp, err := Post(ts.URL, header, reqBody) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dump := resp.dump() | ||
for _, keyword := range []string{reqBody, respBody, reqHeader, respHeader, "cost"} { | ||
if !strings.Contains(dump, keyword) { | ||
t.Errorf("dump missing part, want: %s", keyword) | ||
} | ||
} | ||
} | ||
|
||
func TestDumpUpload(t *testing.T) { | ||
SetFlags(LreqBody) | ||
file1 := ioutil.NopCloser(strings.NewReader("file1")) | ||
uploads := []FileUpload{ | ||
{ | ||
FileName: "1.txt", | ||
FieldName: "media", | ||
File: file1, | ||
}, | ||
} | ||
ts := newDefaultTestServer() | ||
r, err := Post(ts.URL, uploads, Param{"hello": "req"}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dump := r.dump() | ||
contains := []string{ | ||
`Content-Disposition: form-data; name="hello"`, | ||
`Content-Disposition: form-data; name="media"; filename="1.txt"`, | ||
} | ||
for _, contain := range contains { | ||
if !strings.Contains(dump, contain) { | ||
t.Errorf("multipart dump should contains: %s", contain) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,130 @@ | ||
package req | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestToJSON(t *testing.T) { | ||
type Result struct { | ||
Code int `json:"code"` | ||
Msg string `json:"msg"` | ||
} | ||
r1 := Result{ | ||
Code: 1, | ||
Msg: "ok", | ||
} | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
data, _ := json.Marshal(&r1) | ||
w.Write(data) | ||
} | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
r, err := Get(ts.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var r2 Result | ||
err = r.ToJSON(&r2) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if r1 != r2 { | ||
t.Errorf("json response body = %+v; want = %+v", r2, r1) | ||
} | ||
} | ||
|
||
func TestToXML(t *testing.T) { | ||
type Result struct { | ||
XMLName xml.Name | ||
Code int `xml:"code"` | ||
Msg string `xml:"msg"` | ||
} | ||
r1 := Result{ | ||
XMLName: xml.Name{Local: "result"}, | ||
Code: 1, | ||
Msg: "ok", | ||
} | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
data, _ := xml.Marshal(&r1) | ||
w.Write(data) | ||
} | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
r, err := Get(ts.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var r2 Result | ||
err = r.ToXML(&r2) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if r1 != r2 { | ||
t.Errorf("xml response body = %+v; want = %+v", r2, r1) | ||
} | ||
} | ||
|
||
func TestFormat(t *testing.T) { | ||
SetFlags(LstdFlags | Lcost) | ||
reqHeader := "Request-Header" | ||
respHeader := "Response-Header" | ||
reqBody := "request body" | ||
respBody1 := "response body 1" | ||
respBody2 := "response body 2" | ||
respBody := fmt.Sprintf("%s\n%s", respBody1, respBody2) | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set(respHeader, "req") | ||
w.Write([]byte(respBody)) | ||
} | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
|
||
// %v | ||
r, err := Post(ts.URL, reqBody, Header{reqHeader: "hello"}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
str := fmt.Sprintf("%v", r) | ||
for _, keyword := range []string{ts.URL, reqBody, respBody} { | ||
if !strings.Contains(str, keyword) { | ||
t.Errorf("format %%v output lack of part, want: %s", keyword) | ||
} | ||
} | ||
|
||
// %-v | ||
str = fmt.Sprintf("%-v", r) | ||
for _, keyword := range []string{ts.URL, respBody1 + " " + respBody2} { | ||
if !strings.Contains(str, keyword) { | ||
t.Errorf("format %%-v output lack of part, want: %s", keyword) | ||
} | ||
} | ||
|
||
// %+v | ||
str = fmt.Sprintf("%+v", r) | ||
for _, keyword := range []string{reqBody, respBody, reqHeader, respHeader} { | ||
if !strings.Contains(str, keyword) { | ||
t.Errorf("format %%+v output lack of part, want: %s", keyword) | ||
} | ||
} | ||
} | ||
|
||
func TestBytesAndString(t *testing.T) { | ||
respBody := "response body" | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(respBody)) | ||
} | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
r, err := Get(ts.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(r.Bytes()) != respBody { | ||
t.Errorf("response body = %s; want = %s", r.Bytes(), respBody) | ||
} | ||
if r.String() != respBody { | ||
t.Errorf("response body = %s; want = %s", r.String(), respBody) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package req | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func newDefaultTestServer() *httptest.Server { | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("hi")) | ||
} | ||
return httptest.NewServer(http.HandlerFunc(handler)) | ||
} | ||
|
||
func TestSetClient(t *testing.T) { | ||
|
||
ts := newDefaultTestServer() | ||
|
||
client := &http.Client{} | ||
SetClient(client) | ||
_, err := Get(ts.URL) | ||
if err != nil { | ||
t.Errorf("error after set client: %v", err) | ||
} | ||
|
||
SetClient(nil) | ||
_, err = Get(ts.URL) | ||
if err != nil { | ||
t.Errorf("error after set client to nil: %v", err) | ||
} | ||
|
||
client = Client() | ||
if trans, ok := client.Transport.(*http.Transport); ok { | ||
trans.MaxIdleConns = 1 | ||
trans.DisableKeepAlives = true | ||
_, err = Get(ts.URL) | ||
if err != nil { | ||
t.Errorf("error after change client's transport: %v", err) | ||
} | ||
} else { | ||
t.Errorf("transport is not http.Transport: %+#v", client.Transport) | ||
} | ||
} | ||
|
||
func TestSetting(t *testing.T) { | ||
defer func() { | ||
if rc := recover(); rc != nil { | ||
t.Errorf("panic happened while change setting: %v", rc) | ||
} | ||
}() | ||
SetTimeout(2 * time.Second) | ||
EnableCookie(false) | ||
EnableCookie(true) | ||
EnableInsecureTLS(true) | ||
SetJSONIndent("", " ") | ||
SetJSONEscapeHTML(false) | ||
SetXMLIndent("", "\t") | ||
SetProxyUrl("http://localhost:8080") | ||
SetProxy(nil) | ||
} |