diff --git a/server/app_test.go b/server/app_test.go index 17e2989..781d6fb 100644 --- a/server/app_test.go +++ b/server/app_test.go @@ -34,6 +34,7 @@ func startTestApp() { os.RemoveAll(cfg.DataDir) cfg.Addr = "127.0.0.1:16380" + cfg.HttpAddr = "127.0.0.1:21181" os.RemoveAll("/tmp/testdb") diff --git a/server/client_http.go b/server/client_http.go index 4383673..db44c21 100644 --- a/server/client_http.go +++ b/server/client_http.go @@ -45,13 +45,14 @@ func newClientHTTP(app *App, w http.ResponseWriter, r *http.Request) { var err error c := new(httpClient) + c.client = newClient(app) err = c.makeRequest(app, r, w) if err != nil { + c.client.close() w.Write([]byte(err.Error())) return } - c.client = newClient(app) c.perform() c.client.close() } diff --git a/server/client_http_test.go b/server/client_http_test.go new file mode 100644 index 0000000..418a7c0 --- /dev/null +++ b/server/client_http_test.go @@ -0,0 +1,40 @@ +package server + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "testing" +) + +func TestHttp(t *testing.T) { + startTestApp() + + r, err := http.Get(fmt.Sprintf("http://%s/SET/http_hello/world", testApp.cfg.HttpAddr)) + if err != nil { + t.Fatal(err) + } + + ioutil.ReadAll(r.Body) + r.Body.Close() + + r, err = http.Get(fmt.Sprintf("http://%s/GET/http_hello?type=json", testApp.cfg.HttpAddr)) + if err != nil { + t.Fatal(err) + } + + b, _ := ioutil.ReadAll(r.Body) + r.Body.Close() + + var v struct { + Data string `json:"GET"` + } + + if err = json.Unmarshal(b, &v); err != nil { + t.Fatal(err) + } else if v.Data != "world" { + t.Fatal("not equal") + } + +}