Skip to content

Commit

Permalink
More stringent object prepare tests, NewRequestObject function for te…
Browse files Browse the repository at this point in the history
…sting/making http requests
  • Loading branch information
Derek Dowling committed Nov 17, 2015
1 parent e9f6cc8 commit 5e112fe
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
7 changes: 7 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func (o *Object) Unmarshal(objType string, target interface{}) (err SendableErro
// to match the request method type.
func (o *Object) Prepare(r *http.Request) (*Response, SendableError) {

if len(o.ID) == 0 {
return nil, SpecificationError("ID must be set for Object response")
}

if len(o.Type) == 0 {
return nil, SpecificationError("Type must be set for Object response")
}
var status int

switch r.Method {
Expand Down
43 changes: 43 additions & 0 deletions parse.go → request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)

const (
Expand Down Expand Up @@ -138,3 +141,43 @@ func validateRequest(r *http.Request) SendableError {

return nil
}

// NewObjectRequest allows you to create a formatted request that can be used with an
// http.Client or for testing
func NewObjectRequest(method string, baseURL *url.URL, object *Object) (*http.Request, error) {

switch method {
case "PATCH":
case "DELETE":
baseURL.Path = strings.Join([]string{object.Type, object.ID}, "/")
break
case "POST":
break
default:
return nil, SpecificationError(fmt.Sprintf(
"Cannot use HTTP method ''%s' for a JSON Request", method,
))
}

request, err := http.NewRequest(method, baseURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("Error creating new HTTP request: %s", err.Error())
}

// use Prepare to generate a payload
payload, err := object.Prepare(request)
if err != nil {
return nil, fmt.Errorf("Error preparing object: %s", err.Error())
}

content, jsonErr := json.MarshalIndent(payload, "", " ")
if jsonErr != nil {
return nil, fmt.Errorf("Unable to prepare JSON content: %s", jsonErr)
}

request.Header.Add("Content-Type", ContentType)
request.Header.Set("Content-Length", strconv.Itoa(len(content)))
request.Body = createIOCloser(content)

return request, nil
}
25 changes: 25 additions & 0 deletions parse_test.go → request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsh
import (
"encoding/json"
"net/http"
"net/url"
"testing"

. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -108,5 +109,29 @@ func TestParsing(t *testing.T) {
So(vErr.Source.Pointer, ShouldEqual, "data/attributes/id")
})
})

Convey("->NewObjectRequest()", func() {

Convey("should create a valid HTTP request", func() {
url := &url.URL{Host: "test123"}
obj := &Object{ID: "test123", Type: "obj"}
req, err := NewObjectRequest("POST", url, obj)

So(err, ShouldBeNil)
So(req.Method, ShouldEqual, "POST")
So(req.URL, ShouldResemble, url)
})

Convey("should error for invalid HTTP methods", func() {
url := &url.URL{}
obj := &Object{}
_, err := NewObjectRequest("PUT", url, obj)
So(err, ShouldNotBeNil)

singleErr, ok := err.(*Error)
So(ok, ShouldBeTrue)
So(singleErr.Status, ShouldEqual, http.StatusNotAcceptable)
})
})
})
}
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func SendResponse(w http.ResponseWriter, r *http.Request, response *Response) {

content, jsonErr := json.MarshalIndent(response, "", " ")
if jsonErr != nil {
log.Printf("Unable to prepare payload JSON: %s", jsonErr)
log.Printf("Unable to prepare JSON content: %s", jsonErr)
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
}

Expand Down

0 comments on commit 5e112fe

Please sign in to comment.