update
This commit is contained in:
1 parent
0ed2935f04
commit
c8cc0fb28a
11 files changed
+602
-21
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.tornberg.me/go-otel/internal/todos"
|
||||
)
|
||||
|
||||
func TestTodoCreateAndList(t *testing.T) {
|
||||
coll := New(nil, 10, nil)
|
||||
st, err := todos.NewFileStore(filepath.Join(t.TempDir(), "todos.jsonl"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
coll.SetTodoStore(st)
|
||||
|
||||
post := func(body string) *httptest.ResponseRecorder {
|
||||
w := httptest.NewRecorder()
|
||||
coll.HandleTodoCreate(w, httptest.NewRequest(http.MethodPost, "/v1/todos", strings.NewReader(body)))
|
||||
return w
|
||||
}
|
||||
if w := post(`{"project":"a","text":"fix cart","component":"Cart","path":"/cart","hash":"#x"}`); w.Code != http.StatusCreated {
|
||||
t.Fatalf("create: %d %s", w.Code, w.Body)
|
||||
}
|
||||
post(`{"project":"b","text":"other"}`)
|
||||
if w := post(`{"project":"a","text":" "}`); w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("empty text: %d", w.Code)
|
||||
}
|
||||
|
||||
list := func(q string) string {
|
||||
w := httptest.NewRecorder()
|
||||
coll.HandleTodoList(w, httptest.NewRequest(http.MethodGet, "/api/todos"+q, nil))
|
||||
return w.Body.String()
|
||||
}
|
||||
if got := list("?project=a"); !strings.Contains(got, "fix cart") || strings.Contains(got, "other") || !strings.Contains(got, `"hash":"#x"`) {
|
||||
t.Fatalf("project filter: %s", got)
|
||||
}
|
||||
if got := list(""); !strings.Contains(got, "fix cart") || !strings.Contains(got, "other") {
|
||||
t.Fatalf("all: %s", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user