Skip to content

Commit

Permalink
Merge pull request #86 from tsawler/development
Browse files Browse the repository at this point in the history
Add test for WriteXML
  • Loading branch information
tsawler authored Apr 6, 2023
2 parents 783852b + 8716591 commit 967d604
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type JSONResponse struct {
Data interface{} `json:"data,omitempty"`
}

// XMLResponse is the type used for sending JSON around.
type XMLResponse struct {
Error bool `xml:"error"`
Message string `xml:"message"`
Data interface{} `xml:"data,omitempty"`
}

// ReadJSON tries to read the body of a request and converts it into JSON.
func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data interface{}) error {
maxBytes := 1024 * 1024 // one megabyte
Expand Down Expand Up @@ -342,7 +349,7 @@ func (t *Tools) Slugify(s string) (string, error) {
return slug, nil
}

// WriteXML takes a response status code and arbitrary data and writes a json response to the client.
// WriteXML takes a response status code and arbitrary data and writes an XML response to the client.
func (t *Tools) WriteXML(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error {
out, err := xml.Marshal(data)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,21 @@ func TestTools_Slugify(t *testing.T) {
}
}
}

func TestTools_WriteXML(t *testing.T) {
// create a variable of type toolbox.Tools, and just use the defaults.
var testTools Tools

rr := httptest.NewRecorder()
payload := XMLResponse{
Error: false,
Message: "foo",
}

headers := make(http.Header)
headers.Add("FOO", "BAR")
err := testTools.WriteXML(rr, http.StatusOK, payload, headers)
if err != nil {
t.Errorf("failed to write XML: %v", err)
}
}

0 comments on commit 967d604

Please sign in to comment.