grim/resticide

Correctly support query parameters
develop
2016-07-09, Gary Kramlich
92fb02aef02c
Correctly support query parameters
package test
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
)
func buildBody(t *testing.T, r *Request, body string, contentType string) {
test := &Test{}
b, ct := r.buildBody(test)
if ct != contentType {
t.Errorf("got content-type '%s' expected '%s'", ct, contentType)
}
buf := new(bytes.Buffer)
buf.ReadFrom(b)
if buf.String() != body {
t.Errorf("got body of '%s' expected '%s'", buf.String(), body)
}
}
func TestRequestBuildBodyNormal(t *testing.T) {
r := &Request{}
r.Body = "test string"
buildBody(t, r, "test string", "application/html")
}
func TestRequestBuildBodyEmpty(t *testing.T) {
r := &Request{}
buildBody(t, r, "", "")
}
func TestRequestBuildBodyJson(t *testing.T) {
r := &Request{}
j := map[string]interface{}{
"foo": 123,
"bar": true,
"baz": map[string]interface{}{
"a": 789,
},
}
r.JSON = j
test := &Test{}
b, ct := r.buildBody(test)
if ct != "application/json" {
t.Errorf("got content-type '%s' expected 'application/json'", ct)
}
bb, err := ioutil.ReadAll(b)
if err != nil {
t.Errorf("failed: '%s'", err.Error())
}
var got map[string]interface{}
err = json.Unmarshal(bb, &got)
if err != nil {
t.Errorf("failed: '%s'", err.Error())
}
g, _ := json.Marshal(got)
e, _ := json.Marshal(j)
if string(g) != string(e) {
t.Errorf("'%s' != '%s'", g, e)
}
}