-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating terminology, working parse tests
- Loading branch information
Derek Dowling
committed
Nov 11, 2015
1 parent
e8d4a65
commit 32a89e7
Showing
6 changed files
with
198 additions
and
99 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
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,53 @@ | ||
package japi | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestRequest(t *testing.T) { | ||
|
||
Convey("Request Tests", t, func() { | ||
|
||
Convey("->ParseObject()", func() { | ||
objectJSON := `{"data": {"type": "user", "id": "sweetID123", "attributes": {"ID":"123"}}}` | ||
|
||
closer := createIOCloser([]byte(objectJSON)) | ||
|
||
object, err := ParseObject(closer) | ||
So(err, ShouldBeNil) | ||
So(object, ShouldNotBeEmpty) | ||
So(object.Type, ShouldEqual, "user") | ||
So(object.ID, ShouldEqual, "sweetID123") | ||
So(object.Attributes, ShouldResemble, map[string]interface{}{"ID": "123"}) | ||
}) | ||
|
||
Convey("->ParseList()", func() { | ||
listJSON := | ||
`{"data": [ | ||
{"type": "user", "id": "sweetID123", "attributes": {"ID": "123"}}, | ||
{"type": "user", "id": "sweetID456", "attributes": {"ID": "456"}} | ||
]}` | ||
|
||
closer := createIOCloser([]byte(listJSON)) | ||
|
||
list, err := ParseList(closer) | ||
So(err, ShouldBeNil) | ||
So(len(list), ShouldEqual, 2) | ||
|
||
object := list[1] | ||
So(object.Type, ShouldEqual, "user") | ||
So(object.ID, ShouldEqual, "sweetID456") | ||
So(object.Attributes, ShouldResemble, map[string]interface{}{"ID": "456"}) | ||
}) | ||
}) | ||
} | ||
|
||
func createIOCloser(data []byte) io.ReadCloser { | ||
reader := bytes.NewReader(data) | ||
return ioutil.NopCloser(reader) | ||
} |
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 was deleted.
Oops, something went wrong.
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,81 @@ | ||
package japi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// Data represents the top level json format of incoming requests | ||
// and outgoing responses | ||
type Data struct { | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
// ErrorResponse for API requests | ||
type ErrorResponse struct { | ||
Errors []*Error `json:"errors"` | ||
} | ||
|
||
// SendObject sends a single data object as a JSON response | ||
func SendObject(w http.ResponseWriter, status int, object *Object) error { | ||
return Send(w, status, prepareObject(object)) | ||
} | ||
|
||
// SendList sends a list of data objects as a JSON response | ||
func SendList(w http.ResponseWriter, status int, list []*Object) error { | ||
return Send(w, status, prepareList(list)) | ||
} | ||
|
||
// SendError is a convenience function that puts an error into an array | ||
// and then calls SendErrors which is the correct error response format | ||
func SendError(w http.ResponseWriter, err *Error) error { | ||
return SendErrors(w, prepareError(err)) | ||
} | ||
|
||
// SendErrors sends the expected error response format for a | ||
// request that cannot be fulfilled in someway. Allows the user | ||
// to compile multiple errors that can be sent back to a user. Uses | ||
// the first error status as the HTTP Status to return. | ||
func SendErrors(w http.ResponseWriter, errors []*Error) error { | ||
|
||
if len(errors) == 0 { | ||
return fmt.Errorf("No errors provided for attempted error response.") | ||
} | ||
|
||
// use the first error to set the error status | ||
status := errors[0].Status | ||
return Send(w, status, prepareErrorList(errors)) | ||
} | ||
|
||
// Send formats a JSON response with the appropriate headers. | ||
func Send(w http.ResponseWriter, status int, payload interface{}) error { | ||
content, err := json.MarshalIndent(payload, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.Header().Add("Content-Type", ContentType) | ||
w.Header().Set("Content-Length", strconv.Itoa(len(content))) | ||
w.WriteHeader(status) | ||
w.Write(content) | ||
|
||
return nil | ||
} | ||
|
||
func prepareError(err *Error) []*Error { | ||
return []*Error{err} | ||
} | ||
|
||
func prepareErrorList(errors []*Error) *ErrorResponse { | ||
return &ErrorResponse{Errors: errors} | ||
} | ||
|
||
func prepareObject(object *Object) *Data { | ||
return &Data{Data: object} | ||
} | ||
|
||
func prepareList(list []*Object) *Data { | ||
return &Data{Data: list} | ||
} |
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,24 @@ | ||
package japi | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestSend(t *testing.T) { | ||
|
||
Convey("Send Tests", t, func() { | ||
|
||
response := httptest.NewRecorder() | ||
|
||
Convey("->SendObject()", func() { | ||
|
||
}) | ||
|
||
Convey("->SendList()", func() { | ||
|
||
}) | ||
}) | ||
} |