grim/resticide

some test tweaks
develop
2015-11-23, Gary Kramlich
b65a913dd40a
Parents c93371e7ddb1
Children 1c8234575af0
some test tweaks
--- a/test_request_test.go Sat Nov 14 10:38:40 2015 -0600
+++ b/test_request_test.go Mon Nov 23 22:34:15 2015 -0600
@@ -1,6 +1,9 @@
package main
import (
+ "bytes"
+ "encoding/json"
+ "io/ioutil"
"reflect"
"testing"
)
@@ -34,3 +37,69 @@
}
}
}
+
+func testBuildBody(t *testing.T, r *TestRequest, body string, content_type string) {
+ test := &Test{}
+
+ b, ct := r.buildBody(test)
+
+ if ct != content_type {
+ t.Errorf("got content-type '%s' expected '%s'", ct, content_type)
+ }
+
+ buf := new(bytes.Buffer)
+ buf.ReadFrom(b)
+ if buf.String() != body {
+ t.Errorf("got body of '%s' expected '%s'", buf.String(), body)
+ }
+}
+
+func TestRequestBuildBodyNormalTest(t *testing.T) {
+ r := &TestRequest{}
+ r.Body = "test string"
+
+ testBuildBody(t, r, "test string", "application/html")
+}
+
+func TestRequestBuildBodyEmptyTest(t *testing.T) {
+ r := &TestRequest{}
+
+ testBuildBody(t, r, "", "")
+}
+
+func TestRequestBuildBodyJsonTest(t *testing.T) {
+ r := &TestRequest{}
+
+ 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)
+ }
+}