MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / TestPut

Function TestPut

protocol/http/http_server_test.go:35–87  ·  view source on GitHub ↗

测试Put方法

(t *testing.T)

Source from the content-addressed store, hash-verified

33
34// 测试Put方法
35func TestPut(t *testing.T) {
36 handler, _ := newHttpHandler()
37 defer handler.Clean()
38 // 创建一个测试用的http server
39 server := httptest.NewServer(http.HandlerFunc(handler.PutHandler))
40 defer server.Close()
41 // 构造请求
42 reqBody := map[string]string{
43 "key": "test_key",
44 "value": "test_value",
45 }
46 reqBytes, _ := json.Marshal(reqBody)
47
48 req, _ := http.NewRequest(http.MethodPut, server.URL, bytes.NewBuffer(reqBytes))
49 req.Header.Set("Content-Type", "application/json")
50
51 // 发送请求
52 resp, err := http.DefaultClient.Do(req)
53 if err != nil {
54 t.Fatalf("could not send request: %v", err)
55 }
56 defer func(Body io.ReadCloser) {
57 err := Body.Close()
58 if err != nil {
59
60 }
61 }(resp.Body)
62
63 // 检查响应
64 if resp.StatusCode != http.StatusOK {
65 t.Errorf("unexpected status code: %d", resp.StatusCode)
66 }
67
68 body, err := io.ReadAll(resp.Body)
69 if err != nil {
70 t.Errorf("ReadAll error: %v", err)
71 }
72 if string(body) != "ok" {
73 t.Errorf("Put error: expected ok, got %s", string(body))
74 }
75
76 // 验证是否put成功
77 valueByte, err := handler.Get([]byte("test_key"))
78 if err != nil {
79 t.Errorf("Get error: %v", err)
80 }
81 value := string(valueByte)
82 if value == "test_value" {
83 t.Logf("Put: test_value and Get: %s", value)
84 } else {
85 t.Errorf("Put error: expected %s, got %s", "test_value", value)
86 }
87}
88
89func TestDel(t *testing.T) {
90 handler, _ := newHttpHandler()

Callers

nothing calls this directly

Calls 5

newHttpHandlerFunction · 0.85
CloseMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
CleanMethod · 0.45

Tested by

no test coverage detected