summaryrefslogtreecommitdiffstats
path: root/src/dkv/api/endpointViews_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/dkv/api/endpointViews_test.go')
-rw-r--r--src/dkv/api/endpointViews_test.go182
1 files changed, 168 insertions, 14 deletions
diff --git a/src/dkv/api/endpointViews_test.go b/src/dkv/api/endpointViews_test.go
index f603af4..1f7abf3 100644
--- a/src/dkv/api/endpointViews_test.go
+++ b/src/dkv/api/endpointViews_test.go
@@ -17,7 +17,8 @@
package api
import (
- //"encoding/json"
+ "bytes"
+ "encoding/json"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"net/http"
@@ -27,32 +28,185 @@ import (
func Router() *mux.Router {
router := mux.NewRouter()
- router.HandleFunc("/getconfigs", HandleGETS).Methods("GET")
router.HandleFunc("/loadconfigs", HandlePOST).Methods("POST")
+ router.HandleFunc("/getconfig/{key}", HandleGET).Methods("GET")
+ router.HandleFunc("/deleteconfig/{key}", HandleDELETE).Methods("DELETE")
+ router.HandleFunc("/getconfigs", HandleGETS).Methods("GET")
return router
}
-func TestHandlePOST(t *testing.T) {
- // TODO(sshank)
- assert.Equal(t, 0, 0, "Not passed.")
+func TestHandleGETS(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandleGETS_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
}
func TestHandleGET(t *testing.T) {
- // TODO(sshank)
- assert.Equal(t, 0, 0, "Not passed.")
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
}
-func TestHandleGETS(t *testing.T) {
- getkvOld := getkvs
- defer func() { getkvs = getkvOld }()
+func TestHandleGET_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("GET", "/getconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
- getkvs = func() ([]string, error) {
- return nil, nil
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{
+ FilePath: "default",
+ },
}
- request, _ := http.NewRequest("GET", "/getconfigs", nil)
+ b, _ := json.Marshal(body)
+
+ // json Marshal converts struct to json in Bytes. But bytes doesn't have
+ // io reader needed. So the byte is passed to NewBuffer.
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandlePOST_no_body(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{}
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST_no_filepath(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsul{}
+ KeyValues = &FakeKeyValues{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{},
+ }
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 400, response.Code, "400 response is expected")
+}
+
+func TestHandlePOST_ConsulError(t *testing.T) {
+ oldConsul := Consul
+ oldKeyValues := KeyValues
+
+ Consul = &FakeConsulErr{}
+ KeyValues = &FakeKeyValuesErr{}
+
+ defer func() {
+ Consul = oldConsul
+ KeyValues = oldKeyValues
+ }()
+
+ body := &POSTBodyStruct{
+ Type: &TypeStruct{
+ FilePath: "default",
+ },
+ }
+
+ b, _ := json.Marshal(body)
+
+ request, _ := http.NewRequest("POST", "/loadconfigs", bytes.NewBuffer(b))
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 500, response.Code, "500 response is expected")
+}
+
+func TestHandleDELETE(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsul{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("DELETE", "/deleteconfig/key1", nil)
+ response := httptest.NewRecorder()
+ Router().ServeHTTP(response, request)
+
+ assert.Equal(t, 200, response.Code, "200 response is expected")
+}
+
+func TestHandleDELETE_err(t *testing.T) {
+ oldConsul := Consul
+ Consul = &FakeConsulErr{}
+ defer func() { Consul = oldConsul }()
+
+ request, _ := http.NewRequest("DELETE", "/deleteconfig/key1", nil)
response := httptest.NewRecorder()
Router().ServeHTTP(response, request)
- assert.Equal(t, 200, response.Code, "OK response is expected")
+ assert.Equal(t, 400, response.Code, "400 response is expected")
}