a bit like a table-driven test huh
parent
ef32839cd4
commit
849990d37b
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := "127.0.0.1:54321"
|
||||
|
||||
switch len(os.Args) {
|
||||
case 0:
|
||||
fmt.Fprintln(os.Stderr, "how did you even get here")
|
||||
os.Exit(1)
|
||||
case 1:
|
||||
break
|
||||
case 2:
|
||||
addr = os.Args[1]
|
||||
case 3:
|
||||
addr = fmt.Sprintf("%s:%s", os.Args[1], os.Args[2])
|
||||
default:
|
||||
fmt.Fprintln(os.Stderr, "too many args")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("listening on: %s\n", addr)
|
||||
http.Serve(lis, &server{counters: make(map[string]int)})
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type response struct {
|
||||
OK bool `json:"ok"`
|
||||
Hits int `json:"hits"`
|
||||
}
|
||||
|
||||
type server struct {
|
||||
sync.Mutex
|
||||
counters map[string]int
|
||||
}
|
||||
|
||||
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
hits := s.hit(r.URL.Path)
|
||||
|
||||
fmt.Printf("% 8d %s\n", hits, r.URL.Path)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response{OK: true, Hits: hits})
|
||||
}
|
||||
|
||||
func (s *server) hit(path string) int {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
if s.counters == nil {
|
||||
s.counters = map[string]int{path: 1}
|
||||
return 1
|
||||
}
|
||||
|
||||
s.counters[path]++
|
||||
return s.counters[path]
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
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) {
|
||||
type series []testRequest
|
||||
|
||||
runSeries := func(node *tea.Tree, tests series) *tea.Tree {
|
||||
for i, _ := range tests {
|
||||
node = node.Child(&tests[i])
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
root := tea.New(&testStartServer{})
|
||||
|
||||
runSeries(root, series{
|
||||
{path: "/users/alice", expect: 1},
|
||||
{path: "/users/alice", expect: 2},
|
||||
{path: "/users/alice", expect: 3},
|
||||
{path: "/users/alice", expect: 4},
|
||||
})
|
||||
|
||||
runSeries(root, series{
|
||||
{path: "/users/alice", expect: 1},
|
||||
{path: "/users/bob", expect: 1},
|
||||
{path: "/users/alice", expect: 2},
|
||||
{path: "/users/alice", expect: 3},
|
||||
{path: "/users/bob", expect: 2},
|
||||
})
|
||||
|
||||
tea.Run(t, root)
|
||||
}
|
Loading…
Reference in New Issue