package main import ( "net/http" "net/http/httptest" "os" "path" "strings" "testing" ) func TestHTTPPostQuestionAnswers(t *testing.T) { p := path.Join(t.TempDir(), "db.yaml") os.WriteFile(p, []byte("{}"), os.ModePerm) db, err := newYamlDB(p) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() r := httptest.NewRequest( http.MethodPost, "/api/questions/QID/answers", strings.NewReader(`{"answer":"a", "passed": false}`), ) r.SetBasicAuth("u", "") withAuth(withDB(httpPostQuestionAnswers, db))(w, r) if w.Code != http.StatusOK { t.Error(w.Code) } t.Logf("response: %s", w.Body.Bytes()) _, got := db.LastAnswer("u", "QID") if got == (Answer{}) { t.Error("no answer pushed:", got) } t.Logf("answer pushed: %+v", got) }