Initial dumb servers
This commit is contained in:
137
server/serve/routes_test.go
Normal file
137
server/serve/routes_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package serve
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/dynamodb/server/config"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var validKey = "key"
|
||||
var validValue = "value"
|
||||
|
||||
func TestRoutes(t *testing.T) {
|
||||
os.Setenv("DB", "MAP")
|
||||
config.New()
|
||||
s := New()
|
||||
if err := s.Routes(); err != nil {
|
||||
t.Fatalf("cannot routes(): %v", err)
|
||||
}
|
||||
config.Values().DB.Set(validKey, []byte("anything"))
|
||||
|
||||
cases := []struct {
|
||||
path string
|
||||
method string
|
||||
status int
|
||||
}{
|
||||
{
|
||||
path: "/" + validKey,
|
||||
method: "GET",
|
||||
status: http.StatusOK,
|
||||
},
|
||||
{
|
||||
path: "/" + validKey,
|
||||
method: "PUT",
|
||||
status: http.StatusOK,
|
||||
},
|
||||
{
|
||||
path: "/" + validKey,
|
||||
method: "DELETE",
|
||||
status: http.StatusOK,
|
||||
},
|
||||
{
|
||||
path: "/" + validKey,
|
||||
method: "POST",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
method: "GET",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
method: "PUT",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
method: "DELETE",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
method: "POST",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "",
|
||||
method: "GET",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "",
|
||||
method: "PUT",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "",
|
||||
method: "DELETE",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
path: "",
|
||||
method: "POST",
|
||||
status: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
//t.Logf("CASE %v", c)
|
||||
r, err := http.NewRequest(c.method, c.path, strings.NewReader(validValue))
|
||||
if err != nil {
|
||||
t.Fatalf("err making request: %v", err)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.ServeHTTP(w, r)
|
||||
if w.Code != c.status {
|
||||
t.Errorf("wrong status for %q on %q: %v, want %v", c.method, c.path, w.Code, c.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutGet(t *testing.T) {
|
||||
os.Setenv("DB", "MAP")
|
||||
config.New()
|
||||
s := New()
|
||||
if err := s.Routes(); err != nil {
|
||||
t.Errorf("cannot routes(): %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, err := http.NewRequest("PUT", "/"+validKey, strings.NewReader(validValue))
|
||||
if err != nil {
|
||||
t.Fatalf("err making put request: %v", err)
|
||||
}
|
||||
s.put(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("err status on put: %v", w.Code)
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
r, err = http.NewRequest("GET", "/"+validKey, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err making get request: %v", err)
|
||||
}
|
||||
s.get(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("err status on get: %v", w.Code)
|
||||
} else if v, err := ioutil.ReadAll(w.Body); err != nil {
|
||||
t.Fatalf("err reading response body on get: %v", err)
|
||||
} else if string(v) != validValue {
|
||||
t.Fatalf("wrong response body on get: %v", string(v))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user