jarcoal/httpmock

No respond for URL question

zhang-shengping opened this issue · 5 comments

I am using httpmock for testing my REST client recently.

The version of httpmock is github.com/jarcoal/httpmock v1.0.8

I have a piece of test code, it looks not that complicated, but it always panic 2021/12/16 22:17:24 Response err:Get "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123": no responder found

The test code is like below:

func TestGetResources(t *testing.T) {
	httpmock.Activate()
	defer httpmock.DeactivateAndReset()

	host := "test.com"
	path := "/ltm/test_resources/"
	username := "admin"
	password := "password"
	inscure := true

	se := InitSession(host, username, password, inscure)
	se.Client = &http.Client{}
	serv := Service{
		Path:    path,
		Session: se,
	}
	partition := "Project_test123"
	// url := "https://" + host + serv.URIForPartition(partition)
	// url := `https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123` (not work)
	url := "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123"
	// url := "/ltm/test_resources" (not work)

	resp := []byte("hello")

        // I register a byte responder for
        // URL  "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123"
	httpmock.RegisterResponder(
		"GET", url, httpmock.NewBytesResponder(200, resp),
	)

	body := serv.GetResources(partition)

	assert.Equal(t, *body, resp)

}

when I run the test, it always returns panic:

=== RUN   TestGetResources
2021/12/16 22:17:24 Get Reousrces
2021/12/16 22:17:24 Request URL is: https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123
2021/12/16 22:17:24 Response err:Get "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123": no responder found
--- FAIL: TestGetResources (0.00s)
panic: Repsonse err:Get "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123": no responder found [recovered]
        panic: Repsonse err:Get "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123": no responder found

As you can see I log the Request URL is: https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123, it is the same as I registered for the Responder.

Request URL is: https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123
        url := "https://test.com/ltm/test_resources/?$filter=partition%20eq%20Project_test123"

I referred to this issue #106 to try to use /ltm/test_resources to match any path with it. it does not work either.

I got what I am wrong. The same issue as this #95. :(

I register the GET, but in my function, I use Get.

I decided to all use http.MethodGet in my code.

Hello,

I just came to the same conclusion, so that the only way to produce this error in your case is probably to change the method case :)

I let here my simple test: https://go.dev/play/p/BBqdHb2Q0t5

Changing "GET" by "get" line 30 produces your problem.

I will change the "no responder" error message to help in such cases.

@maxatome Thanks a lot. You make this repo great, I referred to your answered issues.

Done in #113

A new release has just been published.