-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to test via Resty httpmock #10
Comments
Hi, @jeremypng. It looks like you only need access to the RestyClient used. I have updated the new branches for DNAC versions 2.2.2.3 and 2.2.3.3. |
It looks like this //examples/test/test_suite_test.go
package main_test
import (
dnac "dnacenter-go-sdk/sdk"
"testing"
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
)
var Client *dnac.Client
// ...
var _ = BeforeSuite(func() {
// block all HTTP requests
Client, _ = dnac.NewClientWithOptions("https://192.168.196.2/",
"altus", "Altus123",
"true", "false")
httpmock.ActivateNonDefault(Client.RestyClient().GetClient())
})
var _ = BeforeEach(func() {
// remove any mocks
httpmock.Reset()
})
var _ = AfterSuite(func() {
httpmock.DeactivateAndReset()
})
func TestBooks(t *testing.T) {
RunSpecs(t, "ApplicationsCount")
} //examples/test/applications_count_test.go
package main_test
import (
"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
)
var _ = Describe("ApplicationsCount", func() {
It("returns a count of applications", func() {
fixture := `{
"response": 1,
"version": "1.0"
}`
responder := httpmock.NewStringResponder(200, fixture)
fakeUrl := "https://192.168.196.2/dna/intent/api/v1/applications-count"
httpmock.RegisterResponder("GET", fakeUrl, responder)
// fetch the article into struct
nResponse, _, err := Client.ApplicationPolicy.GetApplicationsCount()
if err != nil {
Fail("Failure reason 1")
}
if nResponse == nil {
Fail("Failure reason 2")
}
// do stuff with the article object ...
})
}) |
The result $ ginkgo
Running Suite: ApplicationsCount
================================
Random Seed: 1637798439
Will run 1 of 1 specs
•
Ran 1 of 1 Specs in 30.006 seconds
SUCCESS! -- 1 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
2021/11/24 18:01:11.561450 DEBUG RESTY
==============================================================================
~~~ REQUEST ~~~
GET /dna/intent/api/v1/applications-count HTTP/1.1
HOST : 192.168.196.2
HEADERS:
Accept: application/json
Content-Type: application/json
User-Agent: go-resty/2.6.0 (https://github.com/go-resty/resty)
BODY :
***** NO CONTENT *****
------------------------------------------------------------------------------
~~~ RESPONSE ~~~
STATUS : 200
PROTO :
RECEIVED AT : 2021-11-24T18:01:11.561167-06:00
TIME DURATION: 16.917µs
HEADERS :
BODY :
{
"response": 1,
"version": "1.0"
}
==============================================================================
Ginkgo ran 1 suite in 32.441645292s
Test Suite Passed |
For the current version it does not seem to work right now. |
@jeremypng We decided to update the version compatible with Cisco DNA Center v2.1.2 to support this. |
@wastorga - This is failing in testing because line 166 in api_client.go tries to authenticate when instantiating the NewClient() function.
|
Can I get a NewClient() function without the auth method. And then a separate Auth Method? |
Separating Authentication fixes issue #10
Hi, @jeremypng. Thanks for your contribution. |
Separates Authentication in api_client.go into separate func allowing tests without connection to live server
Separates Authentication in api_client.go into separate func allowing tests without connection to live server
I need to write a test for code written using this SDK. Since it is based on Resty, I think the below is how one would go about testing code that uses a Resty client. However, since the first call is NewClient, there is not a way to inject the mock before the call is made to the destination server.
Could the resty class be something that is instantiated before the NewClient or NewClientWithOptions is called so that the mock could be applied and then initiate the connection to DNAC?
If I have misunderstood this please correct me.
Here is an example referenced by the Resty documentation:
https://github.com/jarcoal/httpmock#ginkgo--resty-example
Thanks!
The text was updated successfully, but these errors were encountered: