rest-server/router_test.go

75 lines
2.2 KiB
Go
Raw Normal View History

// +build go1.4
2015-08-14 11:17:57 +02:00
package main
import (
2015-09-17 13:46:49 +02:00
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
2015-08-14 11:17:57 +02:00
"testing"
)
2015-09-17 13:46:49 +02:00
func TestRouter(t *testing.T) {
router := NewRouter()
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getConfig := []byte("GET /config")
2015-09-18 17:13:38 +02:00
router.GetFunc("/config", func(w http.ResponseWriter, r *http.Request) {
2015-09-17 13:46:49 +02:00
w.Write(getConfig)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
postConfig := []byte("POST /config")
2015-09-18 17:13:38 +02:00
router.PostFunc("/config", func(w http.ResponseWriter, r *http.Request) {
2015-09-17 13:46:49 +02:00
w.Write(postConfig)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlobs := []byte("GET /blobs/")
2015-09-18 17:13:38 +02:00
router.GetFunc("/blobs/", func(w http.ResponseWriter, r *http.Request) {
2015-09-17 13:46:49 +02:00
w.Write(getBlobs)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlob := []byte("GET /blobs/:sha")
2015-09-18 17:13:38 +02:00
router.GetFunc("/blobs/:sha", func(w http.ResponseWriter, r *http.Request) {
2015-09-17 13:46:49 +02:00
w.Write(getBlob)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
server := httptest.NewServer(router)
defer server.Close()
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getConfigResp, _ := http.Get(server.URL + "/config")
getConfigBody, _ := ioutil.ReadAll(getConfigResp.Body)
if getConfigResp.StatusCode != 200 {
t.Fatalf("Wanted HTTP Status 200, got %d", getConfigResp.StatusCode)
}
if string(getConfig) != string(getConfigBody) {
t.Fatalf("Config wrong:\nWanted '%s'\nGot: '%s'", string(getConfig), string(getConfigBody))
}
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
postConfigResp, _ := http.Post(server.URL+"/config", "binary/octet-stream", strings.NewReader("post test"))
postConfigBody, _ := ioutil.ReadAll(postConfigResp.Body)
if postConfigResp.StatusCode != 200 {
t.Fatalf("Wanted HTTP Status 200, got %d", postConfigResp.StatusCode)
}
if string(postConfig) != string(postConfigBody) {
t.Fatalf("Config wrong:\nWanted '%s'\nGot: '%s'", string(postConfig), string(postConfigBody))
}
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlobsResp, _ := http.Get(server.URL + "/blobs/")
getBlobsBody, _ := ioutil.ReadAll(getBlobsResp.Body)
if getBlobsResp.StatusCode != 200 {
t.Fatalf("Wanted HTTP Status 200, got %d", getBlobsResp.StatusCode)
}
if string(getBlobs) != string(getBlobsBody) {
t.Fatalf("Config wrong:\nWanted '%s'\nGot: '%s'", string(getBlobs), string(getBlobsBody))
}
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlobResp, _ := http.Get(server.URL + "/blobs/test")
getBlobBody, _ := ioutil.ReadAll(getBlobResp.Body)
if getBlobResp.StatusCode != 200 {
t.Fatalf("Wanted HTTP Status 200, got %d", getBlobResp.StatusCode)
}
if string(getBlob) != string(getBlobBody) {
t.Fatalf("Config wrong:\nWanted '%s'\nGot: '%s'", string(getBlob), string(getBlobBody))
}
2015-08-14 11:17:57 +02:00
}