Skip to content

Commit

Permalink
#17 API streamline and test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Nov 18, 2015
1 parent f6e9419 commit da211ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fmt.Printf("\nError: %v", err)
fmt.Printf("\nResponse Status Code: %v", resp.StatusCode())
fmt.Printf("\nResponse Status: %v", resp.Status())
fmt.Printf("\nResponse Time: %v", resp.Time())
fmt.Printf("\nResponse Recevied At: %v", resp.ReceivedAt)
fmt.Printf("\nResponse Recevied At: %v", resp.ReceivedAt())
fmt.Printf("\nResponse Body: %v", resp)
// more...

Expand Down
34 changes: 22 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,18 +577,18 @@ func (c *Client) execute(req *Request) (*Response, error) {

response := &Response{
Request: req,
ReceivedAt: time.Now(),
RawResponse: resp,
receivedAt: time.Now(),
}

if !req.isSaveResponse {
defer resp.Body.Close()
response.Body, err = ioutil.ReadAll(resp.Body)
response.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

response.size = int64(len(response.Body))
response.size = int64(len(response.body))
}

// Apply Response middleware
Expand Down Expand Up @@ -990,13 +990,18 @@ func (r *Request) Execute(method, url string) (*Response, error) {

// Response is an object represents executed request and its values.
type Response struct {
// Note: `Response.Body` might be nil, if `Request.SetOutput` is used.
Body []byte
ReceivedAt time.Time
Request *Request
RawResponse *http.Response

size int64
body []byte
size int64
receivedAt time.Time
}

// Body method returns HTTP response as []byte array for the executed request.
// Note: `Response.Body` might be nil, if `Request.SetOutput` is used.
func (r *Response) Body() []byte {
return r.body
}

// Status method returns the HTTP status string for the executed request.
Expand Down Expand Up @@ -1033,18 +1038,23 @@ func (r *Response) Cookies() []*http.Cookie {

// String method returns the body of the server response as String.
func (r *Response) String() string {
if r.Body == nil {
if r.body == nil {
return ""
}

return strings.TrimSpace(string(r.Body))
return strings.TrimSpace(string(r.body))
}

// Time method returns the time of HTTP response time that from request we sent and received a request.
// See `response.ReceivedAt` to know when client recevied response and see `response.Request.Time` to know
// when client sent a request.
func (r *Response) Time() time.Duration {
return r.ReceivedAt.Sub(r.Request.Time)
return r.receivedAt.Sub(r.Request.Time)
}

// ReceivedAt method returns when response got recevied from server for the request.
func (r *Response) ReceivedAt() time.Time {
return r.receivedAt
}

// Size method returns the HTTP response size in bytes. Ya, you can relay on HTTP `Content-Length` header,
Expand Down Expand Up @@ -1165,11 +1175,11 @@ func getRequestBodyString(r *Request) (body string) {

func getResponseBodyString(res *Response) string {
bodyStr := "***** NO CONTENT *****"
if res.Body != nil {
if res.body != nil {
ct := res.Header().Get(hdrContentTypeKey)
if IsJSONType(ct) {
var out bytes.Buffer
if err := json.Indent(&out, res.Body, "", " "); err == nil {
if err := json.Indent(&out, res.body, "", " "); err == nil {
bodyStr = string(out.Bytes())
}
} else {
Expand Down
9 changes: 5 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ func responseLogger(c *Client, res *Response) error {
c.Log.Println("")
c.disableLogPrefix()
c.Log.Println("---------------------- RESPONSE LOG -----------------------")
c.Log.Printf("STATUS : %s", res.Status())
c.Log.Printf("TIME : %v", res.Time())
c.Log.Printf("STATUS : %s", res.Status())
c.Log.Printf("RECEIVED AT : %v", res.ReceivedAt())
c.Log.Printf("RESPONSE TIME : %v", res.Time())
c.Log.Println("HEADERS:")
for h, v := range res.Header() {
c.Log.Printf("%30s: %v", h, strings.Join(v, ", "))
Expand All @@ -270,7 +271,7 @@ func parseResponseBody(c *Client, res *Response) (err error) {
// Considered as Result
if res.StatusCode() > 199 && res.StatusCode() < 300 {
if res.Request.Result != nil {
err = Unmarshal(ct, res.Body, res.Request.Result)
err = Unmarshal(ct, res.body, res.Request.Result)
}
}

Expand All @@ -282,7 +283,7 @@ func parseResponseBody(c *Client, res *Response) (err error) {
}

if res.Request.Error != nil {
err = Unmarshal(ct, res.Body, res.Request.Error)
err = Unmarshal(ct, res.body, res.Request.Error)
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func TestOnAfterMiddleware(t *testing.T) {
c := dc()
c.OnAfterResponse(func(c *Client, res *Response) error {
t.Logf("Request sent at: %v", res.Request.Time)
t.Logf("Response Recevied at: %v", res.ReceivedAt)
t.Logf("Response Recevied at: %v", res.ReceivedAt())

return nil
})
Expand Down Expand Up @@ -805,7 +805,7 @@ func TestPatchMethod(t *testing.T) {
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())

resp.Body = nil
resp.body = nil
assertEqual(t, "", resp.String())
}

Expand Down Expand Up @@ -1005,6 +1005,22 @@ func TestOutputFileWithBaseDirError(t *testing.T) {
_ = c
}

func TestOutputPathDirNotExists(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

DefaultClient = dc()
SetRedirectPolicy(FlexibleRedirectPolicy(10))
SetOutputDirectory(getTestDataPath() + "/not-exists-dir")

resp, err := R().
SetOutput("test-img-success.png").
Get(ts.URL + "/my-image.png")

assertError(t, err)
assertEqual(t, true, resp.Size() != 0)
}

func TestOutputFileAbsPath(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down

0 comments on commit da211ca

Please sign in to comment.