Skip to content

Commit

Permalink
http_server_test.go 添加post和getListKeys的测试方法
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchenguang123 committed Jun 28, 2023
1 parent 81f17f5 commit 9e2d21d
Showing 1 changed file with 135 additions and 7 deletions.
142 changes: 135 additions & 7 deletions http/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestPut(t *testing.T) {
t.Errorf("Get error: %v", err)
}
value := string(valueByte)
if value != "test_value" {
t.Errorf("Put error: expected %s, got %s", "test_value", value)
} else {
if value == "test_value" {
t.Logf("Put: test_value and Get: %s", value)
} else {
t.Errorf("Put error: expected %s, got %s", "test_value", value)
}
}

Expand All @@ -98,13 +98,21 @@ func TestDel(t *testing.T) {
req.Header.Set("Content-Type", "multipart/form-data")

//提前插入test_key
handler.Put([]byte("test_key"), []byte("test_value"))
err := handler.Put([]byte("test_key"), []byte("test_value"))
if err != nil {
return
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("could not send request: %v", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

}
}(resp.Body)

// 检查响应
if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -136,14 +144,22 @@ func TestGet(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, server.URL+"?key=test_key", nil)
req.Header.Set("Content-Type", "application/json")
//提前插入test_key
handler.Put([]byte("test_key"), []byte("test_value"))
err := handler.Put([]byte("test_key"), []byte("test_value"))
if err != nil {
return
}

// 发送请求
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("could not send request: %v", err)
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

}
}(resp.Body)
// 检查响应
if resp.StatusCode != http.StatusOK {
t.Errorf("unexpected status code: %d", resp.StatusCode)
Expand All @@ -165,3 +181,115 @@ func TestGet(t *testing.T) {
t.Logf("value:%s", string(val))
}
}

func TestPost(t *testing.T) {
handler, _ := newHttpHandler()
// 创建一个测试用的 HTTP 服务器
server := httptest.NewServer(http.HandlerFunc(handler.PostHandler))
defer server.Close()

// 构造请求
reqBody := map[string]string{
"key": "test_post1",
"value": "test_post1_value",
}
reqBytes, _ := json.Marshal(reqBody)

req, _ := http.NewRequest(http.MethodPost, server.URL, bytes.NewBuffer(reqBytes))
req.Header.Set("Content-Type", "application/json")

// 发送请求
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("could not send request: %v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
// 处理关闭响应主体失败的错误
}
}(resp.Body)

// 检查响应
if resp.StatusCode != http.StatusOK {
t.Errorf("unexpected status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("ReadAll error: %v", err)
}
if string(body) != "ok" {
t.Errorf("Post error: expected ok, got %s", string(body))
}

// 验证是否成功创建新资源
valueByte, err := handler.Get([]byte("test_post"))
if err != nil {
t.Errorf("Get error: %v", err)
}
value := string(valueByte)
if value != "test_post_value" {
t.Errorf("Post error: expected %s, got %s", "test_value", value)
} else {
t.Logf("Post: test_value and Get: %s", value)
}
}

func TestGetListKeysHandler(t *testing.T) {
handler, _ := newHttpHandler()
// 创建一个测试用的http server
server := httptest.NewServer(http.HandlerFunc(handler.GetListKeysHandler))
defer server.Close()
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
req.Header.Set("Content-Type", "application/json")

// 发送请求
resp, err := http.DefaultClient.Do(req)
// 发送请求
if err != nil {
t.Fatalf("could not send request: %v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

}
}(resp.Body)

// 检查响应
if resp.StatusCode != http.StatusOK {
t.Errorf("unexpected status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("ReadAll error: %v", err)
}
// 解析JSON响应
var keys [][]byte
err = json.Unmarshal(body, &keys)
if err != nil {
t.Errorf("JSON unmarshal error: %v", err)
}
// 验证获取的keys是否正确
expectedKeys := handler.GetListKeys()
if len(keys) != len(expectedKeys) {
t.Errorf("unexpected number of keys: expected %d, got %d", len(expectedKeys), len(keys))
}

for i := 0; i < len(keys); i++ {
strA := string(keys[i])
strB := string(expectedKeys[i])
if strA != strB {
t.Errorf("Get error: expected ok, got %s", string(strA))
} else {
value, err2 := handler.Get(keys[i])
if err2 != nil {
return
}
t.Logf("the key of string:%s------the value of the key:%s", string(strA), string(value))
}
}

}

0 comments on commit 9e2d21d

Please sign in to comment.