diff options
-rw-r--r-- | src/kube2msb/msb_client_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/kube2msb/msb_client_test.go b/src/kube2msb/msb_client_test.go index bdc7278..72ff6fb 100644 --- a/src/kube2msb/msb_client_test.go +++ b/src/kube2msb/msb_client_test.go @@ -16,7 +16,9 @@ limitations under the License. package main import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -69,3 +71,50 @@ func TestServiceAnnotation2ServiceUnit(t *testing.T) { t.Errorf("ServiceAnnotation2ServiceUnit error") } } + +func TestRegister(t *testing.T) { + serviceInfo := `[{ + "ip":"192.168.1.10", + "port":"8080", + "serviceName":"resgisterTest", + "version":"v1", + "url":"/register/test", + "protocol":"http", + "lb_policy":"random", + "visualRange":"1", + "path":"rt", + "enable_ssl":true + }]` + + handler := func(res http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + t.Errorf("Register() request method should be 'Post' not %s", req.Method) + } else if urlPrefix != req.URL.String() { + t.Errorf("Register() url should be %s, not %s", urlPrefix, req.URL) + } else { + body, err := ioutil.ReadAll(req.Body) + if err != nil { + t.Errorf("Register() fail to read request body") + } + var su = ServiceUnit{} + parseError := json.Unmarshal([]byte(body), &su) + if parseError != nil { + t.Errorf("Register() request body can not parse to ServiceUnit, %s", body) + return + } else { + res.WriteHeader(200) + res.Header().Set("Content-Type", "application/xml") + fmt.Fprintln(res, "regist success") + } + } + + } + server := httptest.NewServer(http.HandlerFunc(handler)) + defer server.Close() + + client := MSBAgent{ + url: server.URL, + } + + client.Register(serviceInfo) +} |