- Add test code
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Tue, 2 Apr 2019 09:39:08 +0000 (18:39 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Tue, 2 Apr 2019 09:39:08 +0000 (18:39 +0900)
src/restapi/httpclient/http_sender_test.go [new file with mode: 0644]

diff --git a/src/restapi/httpclient/http_sender_test.go b/src/restapi/httpclient/http_sender_test.go
new file mode 100644 (file)
index 0000000..4d58f6e
--- /dev/null
@@ -0,0 +1,71 @@
+package httpclient
+
+import (
+       "encoding/json"
+       "log"
+       "testing"
+
+       "net/http"
+       "net/http/httptest"
+)
+
+var (
+       server         *httptest.Server
+       mockRemoteAddr string
+)
+
+func init() {
+       server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               log.Printf("Test Handler")
+
+               w.WriteHeader(http.StatusOK)
+               w.Write([]byte(r.Method))
+       }))
+
+       mockRemoteAddr = server.URL
+}
+
+func TestDoGet(t *testing.T) {
+       resp, err := DoGet(mockRemoteAddr)
+
+       retError(t, err)
+
+       if string(resp) != "GET" {
+               t.Error()
+       }
+}
+
+func TestDoPost(t *testing.T) {
+       msg := make(map[string]interface{})
+       msg["testKey"] = "test"
+
+       msgBytes, err := json.Marshal(msg)
+       retError(t, err)
+
+       resp, err := DoPost(mockRemoteAddr, msgBytes)
+
+       retError(t, err)
+
+       if string(resp) != "POST" {
+               t.Error()
+       }
+}
+
+func TestDoDelete(t *testing.T) {
+       resp, err := DoDelete(mockRemoteAddr)
+
+       retError(t, err)
+
+       if string(resp) != "DELETE" {
+               t.Error()
+       }
+
+}
+
+func retError(t *testing.T, err error) {
+       t.Helper()
+
+       if err != nil {
+               t.Error(err.Error())
+       }
+}