Add a test

master
breel 2020-07-31 20:34:31 -06:00
parent 5de5fa97ed
commit 1845fdd75f
1 changed files with 110 additions and 0 deletions

View File

@ -284,6 +284,37 @@ func TestWho(t *testing.T) {
t.Logf("POST POST:\n%s", b)
})
t.Run("post real with spaces, #, and special chars in name", func(t *testing.T) {
reset()
want.Name = "hello world #1 e ę"
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
t.Fatal(err)
}
url := url.URL{
Scheme: "http",
Host: "me.me.me",
Path: "/who",
RawQuery: (url.Values{"namespace": []string{"col"}, "id": []string{want.Name}}).Encode(),
}
r := httptest.NewRequest(http.MethodPost, url.String(), bytes.NewReader(b))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
o := entity.One{}
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
t.Fatal(err)
}
if o.Name != want.Name {
t.Fatalf("failed to POST specified name: %+v", o)
}
b, _ = json.MarshalIndent(o, "", " ")
t.Logf("POST POST:\n%q\n%s", url.String(), b)
})
t.Run("delete real", func(t *testing.T) {
reset()
r := httptest.NewRequest(http.MethodDelete, "/who?namespace=col&id=NEWBIE"+want.Name, nil)
@ -300,6 +331,85 @@ func TestWho(t *testing.T) {
}
})
t.Run("delete real with %20%23 (' #')", func(t *testing.T) {
reset()
want.Name = "hello world #4"
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
t.Fatal(err)
}
url := url.URL{
Scheme: "http",
Host: "me.me.me",
Path: "/who",
RawQuery: "namespace=col&id=hello%20world%20%234",
}
t.Logf("using url %s", url.String())
r := httptest.NewRequest(http.MethodPost, url.String(), bytes.NewReader(b))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("(%d) %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodDelete, url.String(), nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodGet, url.String(), nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
t.Logf("can post+del %s", url.String())
})
t.Run("delete real with special chars", func(t *testing.T) {
reset()
want.Name = "hello world #1 e ę"
want.Connections = nil
b, err := json.Marshal(want)
if err != nil {
t.Fatal(err)
}
url := url.URL{
Scheme: "http",
Host: "me.me.me",
Path: "/who",
RawQuery: (url.Values{"namespace": []string{"col"}, "id": []string{want.Name}}).Encode(),
}
r := httptest.NewRequest(http.MethodPost, url.String(), bytes.NewReader(b))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
r = httptest.NewRequest(http.MethodDelete, url.String(), nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodGet, url.String(), nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
t.Logf("can post+del %s", url.String())
})
t.Run("delete fake", func(t *testing.T) {
reset()
r := httptest.NewRequest(http.MethodDelete, "/who?namespace=col&id=FAKER"+want.Name, nil)