rest-server/router_test.go

55 lines
1.5 KiB
Go
Raw Normal View History

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
"github.com/stretchr/testify/require"
2015-08-14 11:17:57 +02:00
)
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")
router.Get("/config", func(w http.ResponseWriter, r *http.Request) {
w.Write(getConfig)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
postConfig := []byte("POST /config")
router.Post("/config", func(w http.ResponseWriter, r *http.Request) {
w.Write(postConfig)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlobs := []byte("GET /blobs/")
router.Get("/blobs/", func(w http.ResponseWriter, r *http.Request) {
w.Write(getBlobs)
})
2015-08-14 11:17:57 +02:00
2015-09-17 13:46:49 +02:00
getBlob := []byte("GET /blobs/:sha")
router.Get("/blobs/:sha", func(w http.ResponseWriter, r *http.Request) {
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)
require.Equal(t, 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)
require.Equal(t, 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)
require.Equal(t, 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)
require.Equal(t, string(getBlob), string(getBlobBody))
2015-08-14 11:17:57 +02:00
}