Skip to content

Commit

Permalink
modified test cases for force proxy (#72)
Browse files Browse the repository at this point in the history
* modified test cases for force proxy
  • Loading branch information
AjeyHiremath authored and chinmayb committed Jun 6, 2018
1 parent ef7e5a9 commit 61dc5f9
Showing 1 changed file with 162 additions and 126 deletions.
288 changes: 162 additions & 126 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,58 +88,70 @@ var _ = Describe("Connector", func() {
wrb := WapiRequestBuilder{HostConfig: hostCfg}

Describe("BuildUrl", func() {
It("should return expected url string for CREATE request", func() {
Context("for CREATE request", func() {
objType := "networkview"
ref := ""
returnFields := []string{}
queryParams := QueryParams{forceProxy: false}
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, objType)
FORCED_PROXY:
urlStr := wrb.BuildUrl(CREATE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
if queryParams.forceProxy == false {
queryParams.forceProxy = true // proxy enabled
goto FORCED_PROXY
}

var queryParams QueryParams
It("should return expected url string for CREATE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, objType)
urlStr := wrb.BuildUrl(CREATE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
})
It("should return expected url string for CREATE request when forceProxy is true", func() {
queryParams.forceProxy = true //proxy enabled
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, objType)
urlStr := wrb.BuildUrl(CREATE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))

})
})

It("should return expected url string for GET for the return fields", func() {
Context("for GET request", func() {
objType := "network"
ref := ""
returnFields := []string{"extattrs", "network", "network_view"}

queryParams := QueryParams{forceProxy: false} // disable proxy
returnFieldsStr := "_return_fields" + "=" + url.QueryEscape(strings.Join(returnFields, ","))
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s?%s",
host, port, version, objType, returnFieldsStr)
urlStr := wrb.BuildUrl(GET, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))

queryParams.forceProxy = true // proxy enabled
qry := "_proxy_search=GM"
expectedURLStr = fmt.Sprintf("https://%s:%s/wapi/v%s/%s?%s&%s",
host, port, version, objType, qry, returnFieldsStr)
urlStr = wrb.BuildUrl(GET, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
var queryParams QueryParams
It("should return expected url string for GET for the return fields when forceProxy is false", func() {
queryParams.forceProxy = false // disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s?%s",
host, port, version, objType, returnFieldsStr)
urlStr := wrb.BuildUrl(GET, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
})
It("should return expected url string for GET for the return fields when forceProxy is true", func() {
queryParams.forceProxy = true // proxy enabled
qry := "_proxy_search=GM"
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s?%s&%s",
host, port, version, objType, qry, returnFieldsStr)
urlStr := wrb.BuildUrl(GET, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
})
})

It("should return expected url string for DELETE request", func() {
Context("for DELETE request", func() {
objType := ""
ref := "fixedaddress/ZG5zLmJpbmRfY25h:12.0.10.1/external"
returnFields := []string{}
queryParams := QueryParams{forceProxy: false} //disable the proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, ref)
FORCED_PROXY:
urlStr := wrb.BuildUrl(DELETE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
if queryParams.forceProxy == false {
queryParams.forceProxy = true // proxy enabled
goto FORCED_PROXY
}
var queryParams QueryParams
It("should return expected url string for DELETE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, ref)
urlStr := wrb.BuildUrl(DELETE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
})
It("should return expected url string for DELETE request when forceProxy is true", func() {
queryParams.forceProxy = true //proxy enabled
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, ref)
urlStr := wrb.BuildUrl(DELETE, objType, ref, returnFields, queryParams)
Expect(urlStr).To(Equal(expectedURLStr))
})
})

})

Describe("BuildBody", func() {
Expand Down Expand Up @@ -182,48 +194,66 @@ var _ = Describe("Connector", func() {
})

Describe("BuildRequest", func() {
It("should return expected Http Request for CREATE request", func() {
Context("for CREATE request", func() {
networkView := "private-view"
cidr := "172.22.18.0/24"
eaKey := "Network Name"
eaVal := "yellow-net"
ea := EA{eaKey: eaVal}
nw := NewNetwork(Network{NetviewName: networkView, Cidr: cidr, Ea: ea})
queryParams := QueryParams{forceProxy: false} //disable the proxy
netviewStr := `"network_view":"` + networkView + `"`
networkStr := `"network":"` + cidr + `"`
eaStr := `"extattrs":{"` + eaKey + `":{"value":"` + eaVal + `"}}`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaStr}, ",") + "}"

hostStr := fmt.Sprintf("%s:%s", host, port)
FORCED_PROXY:
req, err := wrb.BuildRequest(CREATE, nw, "", queryParams)
Expect(err).To(BeNil())
Expect(req.Method).To(Equal("POST"))
Expect(req.URL.Host).To(Equal(hostStr))
Expect(req.URL.Path).To(Equal(fmt.Sprintf("/wapi/v%s/%s", version, nw.ObjectType())))
Expect(req.Header["Content-Type"]).To(Equal([]string{"application/json"}))
Expect(req.Host).To(Equal(hostStr))

actualUsername, actualPassword, ok := req.BasicAuth()
Expect(ok).To(BeTrue())
Expect(actualUsername).To(Equal(username))
Expect(actualPassword).To(Equal(password))

bodyLen := 1000
actualBody := make([]byte, bodyLen)
n, rderr := req.Body.Read(actualBody)
_ = req.Body.Close()
Expect(rderr).To(BeNil())
Expect(n < bodyLen).To(BeTrue())

actualBodyStr := string(actualBody[:n])
Expect(actualBodyStr).To(Equal(expectedBodyStr))
if queryParams.forceProxy == false {
queryParams.forceProxy = true // proxy enabled
goto FORCED_PROXY
}
var queryParams QueryParams
It("should return expected Http Request for CREATE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
hostStr := fmt.Sprintf("%s:%s", host, port)
req, err := wrb.BuildRequest(CREATE, nw, "", queryParams)
Expect(err).To(BeNil())
Expect(req.Method).To(Equal("POST"))
Expect(req.URL.Host).To(Equal(hostStr))
Expect(req.URL.Path).To(Equal(fmt.Sprintf("/wapi/v%s/%s", version, nw.ObjectType())))
Expect(req.Header["Content-Type"]).To(Equal([]string{"application/json"}))
Expect(req.Host).To(Equal(hostStr))
actualUsername, actualPassword, ok := req.BasicAuth()
Expect(ok).To(BeTrue())
Expect(actualUsername).To(Equal(username))
Expect(actualPassword).To(Equal(password))
bodyLen := 1000
actualBody := make([]byte, bodyLen)
n, rderr := req.Body.Read(actualBody)
_ = req.Body.Close()
Expect(rderr).To(BeNil())
Expect(n < bodyLen).To(BeTrue())
actualBodyStr := string(actualBody[:n])
Expect(actualBodyStr).To(Equal(expectedBodyStr))
})
It("should return expected Http Request for CREATE request when forceProxy is true", func() {
queryParams.forceProxy = true //proxy enabled
hostStr := fmt.Sprintf("%s:%s", host, port)
req, err := wrb.BuildRequest(CREATE, nw, "", queryParams)
Expect(err).To(BeNil())
Expect(req.Method).To(Equal("POST"))
Expect(req.URL.Host).To(Equal(hostStr))
Expect(req.URL.Path).To(Equal(fmt.Sprintf("/wapi/v%s/%s", version, nw.ObjectType())))
Expect(req.Header["Content-Type"]).To(Equal([]string{"application/json"}))
Expect(req.Host).To(Equal(hostStr))
actualUsername, actualPassword, ok := req.BasicAuth()
Expect(ok).To(BeTrue())
Expect(actualUsername).To(Equal(username))
Expect(actualPassword).To(Equal(password))
bodyLen := 1000
actualBody := make([]byte, bodyLen)
n, rderr := req.Body.Read(actualBody)
_ = req.Body.Close()
Expect(rderr).To(BeNil())
Expect(n < bodyLen).To(BeTrue())
actualBodyStr := string(actualBody[:n])
Expect(actualBodyStr).To(Equal(expectedBodyStr))
})
})

})
})

Expand Down Expand Up @@ -402,71 +432,77 @@ var _ = Describe("Connector", func() {
})
})
Describe("makeRequest", func() {
netviewName := "private-view"
eaKey := "CMP Type"
eaVal := "OpenStack"
ref := ""
queryParams := QueryParams{forceProxy: false} //disable the proxy
netViewObj := NewNetworkView(NetworkView{
Name: netviewName,
Ea: EA{eaKey: eaVal},
})

requestType := RequestType(GET)
eaStr := `"extattrs":{"` + eaKey + `":{"value":"` + eaVal + `"}}`
netviewStr := `"network_view":"` + netviewName + `"`
urlStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, netViewObj.ObjectType())

bodyStr := []byte("{" + strings.Join([]string{netviewStr, eaStr}, ",") + "}")
httpReq, _ := http.NewRequest(requestType.toMethod(), urlStr, bytes.NewBuffer(bodyStr))
frb := &FakeRequestBuilder{
r: requestType,
obj: netViewObj,
ref: "",
Context("for GET request", func() {
netviewName := "private-view"
eaKey := "CMP Type"
eaVal := "OpenStack"
ref := ""
var queryParams QueryParams
netViewObj := NewNetworkView(NetworkView{
Name: netviewName,
Ea: EA{eaKey: eaVal},
})

urlStr: urlStr,
bodyStr: bodyStr,
req: httpReq,
}
requestType := RequestType(GET)
eaStr := `"extattrs":{"` + eaKey + `":{"value":"` + eaVal + `"}}`
netviewStr := `"network_view":"` + netviewName + `"`
urlStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
host, port, version, netViewObj.ObjectType())

bodyStr := []byte("{" + strings.Join([]string{netviewStr, eaStr}, ",") + "}")
httpReq, _ := http.NewRequest(requestType.toMethod(), urlStr, bytes.NewBuffer(bodyStr))
frb := &FakeRequestBuilder{
r: requestType,
obj: netViewObj,
ref: "",

urlStr: urlStr,
bodyStr: bodyStr,
req: httpReq,
}

expectRef := "networkview/ZG5zLm5ldHdvcmtfdmlldyQyMw:global_view/false"
expectObj := NewNetworkView(NetworkView{
Ref: expectRef,
Name: netviewName,
Ea: EA{eaKey: eaVal},
})
expectRes, _ := json.Marshal(expectObj)
expectRef := "networkview/ZG5zLm5ldHdvcmtfdmlldyQyMw:global_view/false"
expectObj := NewNetworkView(NetworkView{
Ref: expectRef,
Name: netviewName,
Ea: EA{eaKey: eaVal},
})
expectRes, _ := json.Marshal(expectObj)

fhr := &FakeHttpRequestor{
config: transportConfig,
fhr := &FakeHttpRequestor{
config: transportConfig,

req: httpReq,
res: expectRes,
}
req: httpReq,
res: expectRes,
}

OrigValidateConnector := ValidateConnector
ValidateConnector = MockValidateConnector
defer func() { ValidateConnector = OrigValidateConnector }()
OrigValidateConnector := ValidateConnector
ValidateConnector = MockValidateConnector
defer func() { ValidateConnector = OrigValidateConnector }()

conn, err := NewConnector(hostConfig, transportConfig,
frb, fhr)
conn, err := NewConnector(hostConfig, transportConfig,
frb, fhr)

if err != nil {
Fail("Error creating Connector")
}
FORCED_PROXY:
It("should return expected object", func() {
if err != nil {
Fail("Error creating Connector")
}
actual := &NetworkView{}
res, err := conn.makeRequest(GET, netViewObj, ref, queryParams)
err = json.Unmarshal(res, &actual)
Expect(err).To(BeNil())
Expect(NewNetworkView(*actual)).To(Equal(expectObj))
It("should return expected object when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
res, err := conn.makeRequest(GET, netViewObj, ref, queryParams)
err = json.Unmarshal(res, &actual)
Expect(err).To(BeNil())
Expect(NewNetworkView(*actual)).To(Equal(expectObj))
})
It("should return expected object when forceProxy is true", func() {
queryParams.forceProxy = true //disable proxy
res, err := conn.makeRequest(GET, netViewObj, ref, queryParams)
err = json.Unmarshal(res, &actual)
Expect(err).To(BeNil())
Expect(NewNetworkView(*actual)).To(Equal(expectObj))
})
})
if queryParams.forceProxy == false {
queryParams.forceProxy = true // proxy enabled
goto FORCED_PROXY
}

})

})
Expand Down

0 comments on commit 61dc5f9

Please sign in to comment.