From 444f9e40d5ce8a3fa71566d1a9844d7bed0bc593 Mon Sep 17 00:00:00 2001 From: Karthic Rao Date: Thu, 27 Aug 2015 23:36:32 +0530 Subject: [PATCH] initial test for replacer --- middleware/replacer_test.go | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 middleware/replacer_test.go diff --git a/middleware/replacer_test.go b/middleware/replacer_test.go new file mode 100644 index 00000000..ad8a9489 --- /dev/null +++ b/middleware/replacer_test.go @@ -0,0 +1,38 @@ +package middleware + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestNewReplacer(t *testing.T) { + w := httptest.NewRecorder() + recordRequest := NewResponseRecorder(w) + userJson := `{"username": "dennis"}` + + reader := strings.NewReader(userJson) //Convert string to reader + + request, err := http.NewRequest("POST", "http://caddyserver.com", reader) //Create request with JSON body + if err != nil { + t.Fatalf("Request Formation Failed \n") + } + replaceValues := NewReplacer(request, recordRequest, "") + + switch v := replaceValues.(type) { + case replacer: + if v.replacements["{host}"] != "caddyserver.com" { + t.Errorf("Expected host to be caddyserver.com") + } + if v.replacements["{method}"] != "POST" { + t.Errorf("Expected request method to be POST") + } + if v.replacements["{status}"] != "200" { + t.Errorf("Expected status to be 200") + } + + default: + t.Fatalf("Return Value from New Replacer expected pass type assertion into a replacer type \n") + } +}