You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
4 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/jordanorelli/tea"
|
||
|
)
|
||
|
|
||
|
type testStartServer struct {
|
||
|
Server *httptest.Server `tea:"save"`
|
||
|
}
|
||
|
|
||
|
func (test *testStartServer) Run(t *testing.T) {
|
||
|
handler := new(server)
|
||
|
test.Server = httptest.NewServer(handler)
|
||
|
t.Logf("started a test server on: %s", test.Server.URL)
|
||
|
}
|
||
|
|
||
|
func (test *testStartServer) After(t *testing.T) {
|
||
|
t.Logf("closing a test server on: %s", test.Server.URL)
|
||
|
test.Server.Close()
|
||
|
}
|
||
|
|
||
|
type testRequest struct {
|
||
|
Server *httptest.Server `tea:"load"`
|
||
|
|
||
|
path string
|
||
|
expect int
|
||
|
}
|
||
|
|
||
|
func (test *testRequest) Run(t *testing.T) {
|
||
|
client := test.Server.Client()
|
||
|
|
||
|
res, err := client.Get(test.Server.URL + test.path)
|
||
|
if err != nil {
|
||
|
t.Fatalf("request to %s failed: %v", test.path, err)
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
|
||
|
var body response
|
||
|
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
|
||
|
t.Fatalf("response at %s was not json: %v", test.path, err)
|
||
|
}
|
||
|
|
||
|
if body.Hits != test.expect {
|
||
|
t.Errorf("expected a count of %d but saw %d", test.expect, body.Hits)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestServer(t *testing.T) {
|
||
4 years ago
|
type list []testRequest
|
||
4 years ago
|
|
||
4 years ago
|
runSeries := func(node *tea.Tree, tests list) *tea.Tree {
|
||
|
for i := 0; i < len(tests); i++ {
|
||
4 years ago
|
node = node.Child(&tests[i])
|
||
|
}
|
||
|
return node
|
||
|
}
|
||
|
|
||
4 years ago
|
runParallel := func(node *tea.Tree, tests list) {
|
||
|
for i := 0; i < len(tests); i++ {
|
||
|
node.Child(&tests[i])
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
root := tea.New(&testStartServer{})
|
||
|
|
||
4 years ago
|
runSeries(root, list{
|
||
4 years ago
|
{path: "/users/alice", expect: 1},
|
||
|
{path: "/users/alice", expect: 2},
|
||
|
{path: "/users/alice", expect: 3},
|
||
|
{path: "/users/alice", expect: 4},
|
||
|
})
|
||
|
|
||
4 years ago
|
runSeries(root, list{
|
||
4 years ago
|
{path: "/users/bob", expect: 1},
|
||
4 years ago
|
{path: "/users/alice", expect: 1},
|
||
4 years ago
|
{path: "/users/alice", expect: 2},
|
||
|
{path: "/users/alice", expect: 3},
|
||
|
{path: "/users/bob", expect: 2},
|
||
|
})
|
||
|
|
||
4 years ago
|
runParallel(root, list{
|
||
|
{path: "/users/alice", expect: 1},
|
||
|
{path: "/users/alice", expect: 1},
|
||
|
{path: "/users/alice", expect: 1},
|
||
|
{path: "/users/alice", expect: 1},
|
||
|
})
|
||
|
|
||
4 years ago
|
tea.Run(t, root)
|
||
|
}
|