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")
|
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)
|
2015-09-18 13:02:20 +02:00
|
|
|
require.Equal(t, 200, getConfigResp.StatusCode)
|
2015-09-17 13:46:49 +02:00
|
|
|
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)
|
2015-09-18 13:02:20 +02:00
|
|
|
require.Equal(t, 200, postConfigResp.StatusCode)
|
2015-09-17 13:46:49 +02:00
|
|
|
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)
|
2015-09-18 13:02:20 +02:00
|
|
|
require.Equal(t, 200, getBlobsResp.StatusCode)
|
2015-09-17 13:46:49 +02:00
|
|
|
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)
|
2015-09-18 13:02:20 +02:00
|
|
|
require.Equal(t, 200, getBlobResp.StatusCode)
|
2015-09-17 13:46:49 +02:00
|
|
|
require.Equal(t, string(getBlob), string(getBlobBody))
|
2015-08-14 11:17:57 +02:00
|
|
|
}
|