- Update http_sender api
authorjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 28 Mar 2019 12:33:48 +0000 (21:33 +0900)
committerjw_wonny.cha <jw_wonny.cha@samsung.com>
Thu, 28 Mar 2019 12:33:48 +0000 (21:33 +0900)
src/servicemgr/http_sender.go
src/servicemgr/http_sender_test.go [new file with mode: 0644]

index 821f667..bb32167 100644 (file)
@@ -2,12 +2,14 @@ package servicemgr
 
 import (
        "bytes"
+       "encoding/json"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
 // SendPostJSONMsg is for sending JSON body
-func SendPostJSONMsg(targetURL string, pbytes []byte) (err error) {
+func sendPostJSONMsg(targetURL string, pbytes []byte) (str string, err error) {
        buff := bytes.NewBuffer(pbytes)
 
        req, err := http.NewRequest("POST", targetURL, buff)
@@ -25,11 +27,17 @@ func SendPostJSONMsg(targetURL string, pbytes []byte) (err error) {
        }
        defer resp.Body.Close()
 
+       log.Println("[Header] : ", resp.Status)
        // Checking Response Validation
        respBody, err := ioutil.ReadAll(resp.Body)
        if err == nil {
-               str := string(respBody)
-               println(str)
+               var responseMsg map[string]interface{}
+               json.Unmarshal(respBody, &responseMsg)
+               log.Println("[JSON] : ", responseMsg)
+
+               str = responseMsg[ConstKeyStatus].(string)
+               log.Println(str)
        }
+
        return
 }
diff --git a/src/servicemgr/http_sender_test.go b/src/servicemgr/http_sender_test.go
new file mode 100644 (file)
index 0000000..cf6cef6
--- /dev/null
@@ -0,0 +1,56 @@
+package servicemgr
+
+import (
+       "encoding/json"
+       "log"
+       "testing"
+
+       "net/http"
+       "net/http/httptest"
+)
+
+var (
+       mockRemoteAddr string
+)
+
+func APIV1ServicemgrServicesPost(w http.ResponseWriter, r *http.Request) {
+       log.Printf("[%s] APIV1ServicemgrServicesPost", logPrefix)
+
+       smbytes, _ := json.Marshal(ServiceExecutionResponse{Status: ConstServiceStatusStarted})
+       writeJSONResponse(w, smbytes, http.StatusOK)
+}
+
+func writeJSONResponse(w http.ResponseWriter, data []byte, status int) {
+       log.Printf("[%s] writeJSONResponse: %s", logPrefix, data)
+       w.Header().Set("Content-Type", "application/json; charset=UTF-8")
+       w.WriteHeader(status)
+       w.Write(data)
+}
+
+func init() {
+       handler := http.HandlerFunc(APIV1ServicemgrServicesPost)
+       server := httptest.NewServer(handler)
+
+       mockRemoteAddr = server.URL
+       log.Println("[URL]", mockRemoteAddr)
+
+       defer server.Close()
+}
+
+func testSendPostJSONMsgs(t *testing.T) {
+       msg := make(map[string]interface{})
+       msg["testKey"] = "test"
+
+       msgBytes, err := json.Marshal(msg)
+       if err != nil {
+               log.Print(err.Error())
+       }
+
+       str, err := sendPostJSONMsg(mockRemoteAddr, msgBytes)
+       log.Println(str)
+
+       if err != nil {
+               log.Println(err.Error())
+               t.Error()
+       }
+}