Skip to content

Commit

Permalink
First test for the http handler
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Dec 6, 2023
1 parent 4f37b29 commit cf9d6bb
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions http_request_test.go
@@ -0,0 +1,57 @@
package veun_test

import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/alecthomas/assert/v2"
"github.com/stanistan/veun"
)

func TestRequestBasicHandler(t *testing.T) {
var handler = veun.HTTPHandler{
veun.RequestRenderableFunc(func(r *http.Request) (veun.AsRenderable, error) {
return nil, nil
}),
}

mux := http.NewServeMux()

mux.Handle("/empty", handler)

server := httptest.NewServer(mux)
defer server.Close()

var sendRequest = func(t *testing.T, to string) (string, int, error) {
t.Helper()

req, err := http.NewRequestWithContext(context.TODO(), "GET", server.URL+to, nil)
assert.NoError(t, err)

res, err := http.DefaultClient.Do(req)
if err != nil {
return "", 0, err
}

defer res.Body.Close()

data, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)

return string(data), res.StatusCode, nil
}

t.Run("the root path is a real server that 404s", func(t *testing.T) {
_, code, _ := sendRequest(t, "/")
assert.Equal(t, 404, code)
})

t.Run("empty handler is indeed empty", func(t *testing.T) {
body, code, _ := sendRequest(t, "/empty")
assert.Equal(t, "", body)
assert.Equal(t, 200, code)
})
}

0 comments on commit cf9d6bb

Please sign in to comment.