jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

Add sorted query feature #50

Closed konalegi closed 5 years ago

konalegi commented 6 years ago

Here is the new feature for mocking different mock requests. Some go libs make HTTP requests with different query items in url. example for huandu/facebook lib:

// sometimes:
GET http://graph.facebook.com/insights?ids=1,2,3&access_token=123123
// sometimes:
GET http://graph.facebook.com/insights?access_token=123123&ids=1,2,3

The current mock implementation doesn't allow handle this types of requests. My PR allows to pass query items as map and it will always match despite of order.

Usage example:

func TestFetchArticles(t *testing.T) {
    httpmock.Activate()
    httpmock.DeactivateAndReset()
    expecedQuery := map[string]string{
        "a": "1",
        "b": "2"
  }

    httpmock.RegisterResponderWithQuery("GET", "http://example.com/", expecedQuery,
        httpmock.NewStringResponder("hello world", 200))

    // requests to http://example.com?a=1&b=2 and http://example.com?b=2&a=1 will now return 'hello world'
}

We are using my modification in production.

konalegi commented 5 years ago

@dlebech done.

konalegi commented 5 years ago

@dlebech any feedback?