package main import ( "bytes" "encoding/json" "net/http" "strconv" "testing" "time" ) func TestReadIssueHandler(t *testing.T) { app := newTestApplication(t) ts := newTestServer(t, app, app.routes()) defer ts.Close() token := ts.registerAndLogin(t, uniqueEmail(), "pa$$word123") req := map[string]any{ "title": "An old silent pond...", "description": "A frog jumps into the pond", "start_time": time.Now().Format(time.RFC3339), "end_time": time.Now().Add(24 * time.Hour).Format(time.RFC3339), } code, _, res := ts.postJSONWithToken(t, "/v1/issues", token, req) if code != http.StatusCreated { t.Fatalf("seed issue: want 201 got %d: %s", code, res) } var resp struct { Issue struct { ID int64 `json:"id"` } `json:"issue"` } json.Unmarshal(res, &resp) issueID := resp.Issue.ID tests := []struct { name string urlPath string wantCode int wantBody []byte }{ {"Valid ID", "/v1/issues/" + strconv.Itoa(int(issueID)), http.StatusOK, []byte("An old silent pond...")}, {"Non-existent ID", "/v1/issues/" + strconv.Itoa(int(issueID + 1)), http.StatusNotFound, nil}, {"Negative ID", "/v1/issues/-1", http.StatusNotFound, nil}, {"Decimal ID", "/v1/issues/1.23", http.StatusNotFound, nil}, {"String ID", "/v1/issues/foo", http.StatusNotFound, nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { code, _, body := ts.getWithToken(t, tt.urlPath, token) if code != tt.wantCode { t.Errorf("want %d; got %d", tt.wantCode, code) } if !bytes.Contains(body, tt.wantBody) { t.Errorf("want body to contain %q; got %q", tt.wantBody, string(body)) } }) } }