fix http panic error

This commit is contained in:
siddontang 2014-12-26 10:08:37 +08:00
parent 8d94615ee4
commit 8c75a693f4
3 changed files with 43 additions and 1 deletions

View File

@ -34,6 +34,7 @@ func startTestApp() {
os.RemoveAll(cfg.DataDir) os.RemoveAll(cfg.DataDir)
cfg.Addr = "127.0.0.1:16380" cfg.Addr = "127.0.0.1:16380"
cfg.HttpAddr = "127.0.0.1:21181"
os.RemoveAll("/tmp/testdb") os.RemoveAll("/tmp/testdb")

View File

@ -45,13 +45,14 @@ func newClientHTTP(app *App, w http.ResponseWriter, r *http.Request) {
var err error var err error
c := new(httpClient) c := new(httpClient)
c.client = newClient(app)
err = c.makeRequest(app, r, w) err = c.makeRequest(app, r, w)
if err != nil { if err != nil {
c.client.close()
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
c.client = newClient(app)
c.perform() c.perform()
c.client.close() c.client.close()
} }

View File

@ -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")
}
}