Skip to content

Commit

Permalink
added test for reloading malformed JSON while maintaining API list
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Nov 5, 2018
1 parent 8156e63 commit 87c6849
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"github.com/gorilla/mux"
"testing"
"time"

Expand Down Expand Up @@ -59,6 +60,105 @@ func stopRPCMock(server *gorpc.Server) {
rpc.Reset()
}

const apiDefListTest = `[{
"api_id": "1",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
}]`

const apiDefListTest2 = `[{
"api_id": "1",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v1": {"name": "v1"}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
},
{
"api_id": "2",
"definition": {
"location": "header",
"key": "version"
},
"auth": {"auth_header_name": "authorization"},
"version_data": {
"versions": {
"v2": {"name": "v2"}
}
},
"proxy": {
"listen_path": "/v2",
"target_url": "` + testHttpAny + `"
}
}]`

func TestSyncAPISpecsRPCFailure_CheckGlobals(t *testing.T) {
// Mock RPC
callCount := 0
dispatcher := gorpc.NewDispatcher()
dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) {
if callCount == 0 {
callCount += 1
return apiDefListTest, nil
}

if callCount == 1 {
callCount += 1
return apiDefListTest2, nil
}

return "malformed json", nil
})
dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool {
return true
})
dispatcher.AddFunc("GetPolicies", func(orgId string) (string, error) {
return `[]`, nil
})

rpc := startRPCMock(dispatcher)
defer stopRPCMock(rpc)

// Three cases: 1 API, 2 APIs and Malformed data
exp := []int{4, 6, 6}

for _, e := range exp {
doReload()

rtCnt := 0
mainRouter.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
rtCnt += 1
//fmt.Println(route.GetPathTemplate())
return nil
})

if rtCnt != e {
t.Errorf("There should be %v routes, got %v", e, rtCnt)
}
}
}

// Our RPC layer too racy, but not harmul, mostly global variables like RPCIsClientConnected
func TestSyncAPISpecsRPCFailure(t *testing.T) {
// Mock RPC
Expand Down

0 comments on commit 87c6849

Please sign in to comment.