Download task_047/tests/review_server_test.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/tests/review_server_test.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_047/tests/review_server_test.go
-
curl -L -o review_server_test.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/tests/review_server_test.go
2.72 kB
| package loganalyzer | |
| import ( | |
| "bytes" | |
| "encoding/json" | |
| "io" | |
| "net" | |
| "net/http" | |
| "os" | |
| "os/exec" | |
| "path/filepath" | |
| "strings" | |
| "testing" | |
| "time" | |
| ) | |
| func TestReviewHTTPServer(t *testing.T) { | |
| binary := filepath.Join(t.TempDir(), "server") | |
| if output, err := exec.Command("go", "build", "-o", binary, "./cmd/server").CombinedOutput(); err != nil { | |
| t.Fatalf("server build: %v %s", err, output) | |
| } | |
| listener, err := net.Listen("tcp", "127.0.0.1:0") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| port := strings.Split(listener.Addr().String(), ":")[1] | |
| listener.Close() | |
| store := filepath.Join(t.TempDir(), "logs.jsonl") | |
| start := func() *exec.Cmd { | |
| command := exec.Command(binary) | |
| command.Env = append(os.Environ(), "PORT="+port, "STORAGE=file", "LOG_FILE="+store) | |
| if err := command.Start(); err != nil { | |
| t.Fatal(err) | |
| } | |
| t.Cleanup(func() { command.Process.Kill(); command.Wait() }) | |
| for i := 0; i < 100; i++ { | |
| conn, err := net.DialTimeout("tcp", "127.0.0.1:"+port, 20*time.Millisecond) | |
| if err == nil { | |
| conn.Close() | |
| return command | |
| } | |
| time.Sleep(20 * time.Millisecond) | |
| } | |
| t.Fatal("server did not bind configured port") | |
| return command | |
| } | |
| client := &http.Client{Timeout: 3 * time.Second} | |
| base := "http://127.0.0.1:" + port | |
| request := func(method, path, body string) (int, []byte) { | |
| t.Helper() | |
| req, _ := http.NewRequest(method, base+path, strings.NewReader(body)) | |
| response, err := client.Do(req) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer response.Body.Close() | |
| raw, _ := io.ReadAll(response.Body) | |
| return response.StatusCode, raw | |
| } | |
| command := start() | |
| item := Log{ID: "large-valid", Message: strings.Repeat("x", 100*1024), Level: "INFO", Timestamp: "2025-09-05T12:00:00Z"} | |
| raw, _ := json.Marshal(item) | |
| for _, suffix := range []string{"{}", "garbage"} { | |
| code, _ := request("POST", "/logs", string(raw)+suffix) | |
| if code != 400 { | |
| t.Fatalf("trailing JSON status=%d", code) | |
| } | |
| } | |
| if code, _ := request("GET", "/log/large-valid", ""); code != 404 { | |
| t.Fatal("invalid record became queryable") | |
| } | |
| if code, _ := request("POST", "/logs", string(raw)+" \n"); code < 200 || code >= 300 { | |
| t.Fatalf("valid POST=%d", code) | |
| } | |
| for _, route := range []string{"/logs", "/log/large-valid"} { | |
| if code, _ := request("DELETE", route, ""); code != 405 { | |
| t.Fatalf("method %s=%d", route, code) | |
| } | |
| } | |
| command.Process.Kill() | |
| command.Wait() | |
| start() | |
| code, data := request("GET", "/log/large-valid", "") | |
| var got Log | |
| if code != 200 || json.Unmarshal(data, &got) != nil || got.Message != item.Message || got.Tag == "" { | |
| t.Fatalf("persisted large log mismatch status=%d bytes=%d", code, len(data)) | |
| } | |
| if !bytes.Contains(data, []byte(item.ID)) { | |
| t.Fatal("incorrect record returned") | |
| } | |
| } | |