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

Function TestGet

protocol/http/http_server_test.go:137–182  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

135}
136
137func TestGet(t *testing.T) {
138 handler, _ := newHttpHandler()
139 defer handler.Clean()
140 // 创建一个测试用的http server
141 server := httptest.NewServer(http.HandlerFunc(handler.GetHandler))
142 defer server.Close()
143 req, _ := http.NewRequest(http.MethodGet, server.URL+"?key=test_key", nil)
144 req.Header.Set("Content-Type", "application/json")
145 //提前插入test_key
146 err := handler.Put([]byte("test_key"), []byte("test_value"))
147 if err != nil {
148 return
149 }
150
151 // 发送请求
152 resp, err := http.DefaultClient.Do(req)
153 if err != nil {
154 t.Fatalf("could not send request: %v", err)
155 }
156 defer func(Body io.ReadCloser) {
157 err := Body.Close()
158 if err != nil {
159
160 }
161 }(resp.Body)
162 // 检查响应
163 if resp.StatusCode != http.StatusOK {
164 t.Errorf("unexpected status code: %d", resp.StatusCode)
165 }
166
167 body, err := io.ReadAll(resp.Body)
168 if err != nil {
169 t.Errorf("ReadAll error: %v", err)
170 }
171 // 验证是否Get成功
172 val, err := handler.Get([]byte("test_key"))
173 if err != nil {
174 t.Errorf("Get error: %v", err)
175 }
176
177 if string(body) != string(val) {
178 t.Errorf("Get error: expected ok, got %s", string(body))
179 } else {
180 t.Logf("value:%s", string(val))
181 }
182}
183
184func TestPost(t *testing.T) {
185 handler, _ := newHttpHandler()

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected