Skip to content

Commit

Permalink
make response available in the script
Browse files Browse the repository at this point in the history
  • Loading branch information
speier committed Nov 25, 2020
1 parent b5d6394 commit 58753a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.1.5
VERSION=v0.1.6

release:
@git tag -a ${VERSION} -m "Release ${VERSION}" && git push origin ${VERSION}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ GET(`${host}/get?foo=bar`)
POST(`${host}/post`, 'Accept: application/vnd.xxx.v2+json', { foo: { bar: 'baz' }})

// expect status code
expect(200, GET(`${host}/status/400`))
expect(200, GET(`${host}/status/400`).statusCode)
8 changes: 4 additions & 4 deletions pkg/req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func printres(res *Response, err error) {
fmt.Println(res)
}

func httpGet(host string, args ...interface{}) int {
func httpGet(host string, args ...interface{}) interface{} {
res, err := httpreq(http.MethodGet, host, args...)
printres(res, err)
return res.StatusCode
return res.Map()
}

func httpPost(host string, args ...interface{}) int {
func httpPost(host string, args ...interface{}) interface{} {
res, err := httpreq(http.MethodPost, host, args...)
printres(res, err)
return res.StatusCode
return res.Map()
}

func httpreq(method string, host string, args ...interface{}) (*Response, error) {
Expand Down
38 changes: 26 additions & 12 deletions pkg/req/res.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package req

import (
"encoding/json"
"fmt"
"net/http"
"strings"
)

// minimal http.Response
type Response struct {
Host string
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
Header http.Header
Body string
ContentLength int64
TransferEncoding []string
Uncompressed bool
Trailer http.Header
Host string `json:"host"`
Status string `json:"status"` // e.g. "200 OK"
StatusCode int `json:"statusCode"` // e.g. 200
Proto string `json:"proto"` // e.g. "HTTP/1.0"
ProtoMajor int `json:"protoMajor"` // e.g. 1
ProtoMinor int `json:"protoMinor"` // e.g. 0
Header http.Header `json:"header"`
Body string `json:"body"`
ContentLength int64 `json:"contentLength"`
TransferEncoding []string `json:"transferEncoding"`
Uncompressed bool `json:"uncompressed"`
Trailer http.Header `json:"trailer"`
}

func (r *Response) String() string {
Expand All @@ -28,3 +29,16 @@ func (r *Response) String() string {
fmt.Fprintf(sb, "%s\n", r.Body)
return sb.String()
}

func (r *Response) Map() map[string]interface{} {
b, err := json.Marshal(&r)
if err != nil {
panic(err)
}
var m map[string]interface{}
err = json.Unmarshal(b, &m)
if err != nil {
panic(err)
}
return m
}

0 comments on commit 58753a8

Please sign in to comment.