Skip to content
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

Unable to use specific query parameters with custom cassettes #13

Closed
msawka opened this issue Nov 11, 2014 · 5 comments
Closed

Unable to use specific query parameters with custom cassettes #13

msawka opened this issue Nov 11, 2014 · 5 comments

Comments

@msawka
Copy link

msawka commented Nov 11, 2014

There seems to be an issue with the Regex pattern matching in the handler.ex's matching
in which URLs that contain query paramters aren't able to be used (params after the ? will break the match). Here's an example:

test "handles failure" do
  use_cassette "failure", custom: true do
    case HTTPoison.post("https://myurl.com?auth_token=123abc", "", [], []) do
      {:ok, %HTTPoison.Response{status_code: 204}} ->
        Logger.debug("Success")
      {:error, %HTTPoison.Error{id: _, reason: reason}} ->
        Logger.error("Failure")
    end
  end    
end

failure.json:

[
  {
    "request": {
      "body": "",
      "headers": {
        "Content-Type": "application/json"
      },
      "method": "post",
      "options": {
        "connect_timeout": 5000
      },
      "url": "https://myurl.com?auth_token=123abc"
    },
    "response": {
      "body": "",
      "headers": {
      },
      "status_code": 400,
      "type": "error"
    }
  }
]

A work-around is to use regex in the JSON to ignore the query params, or to simply leave the query params off:
failure.json:

[
  {
    "request": {
      "body": "",
      "headers": {
        "Content-Type": "application/json"
      },
      "method": "post",
      "options": {
        "connect_timeout": 5000
      },
      "url": "https://myurl.com.+"
    },
    "response": {
      "body": "",
      "headers": {
      },
      "status_code": 400,
      "type": "error"
    }
  }
]

But, this can be problematic if you need to ensure the query params are set correctly. Adding a Regex.escape (see below) seems to resolve the issue for this case, but looks like it breaks other pattern matching in URLs within the JSON:

pattern = Regex.compile!("^#{Regex.escape(response[:request].url)}.*$")
@parroty
Copy link
Owner

parroty commented Dec 10, 2014

Thanks for the report. I'll be checking.

@parroty
Copy link
Owner

parroty commented Dec 10, 2014

Maybe having regex by default is not good approach. Specifying it as regex ~r/https://myurl.com.+/ syntax makes sense?

@msawka
Copy link
Author

msawka commented Jan 23, 2015

Sorry for the delayed response, i think that makes a lot of sense

@parroty
Copy link
Owner

parroty commented Feb 4, 2015

Thanks for the reply. I've pushed a related commit. I think this allows either String or Regexp, though the format is getting tricker..

@parroty
Copy link
Owner

parroty commented Feb 15, 2015

I've pushed as v0.4.0, and I'm closing this item at the moment. If you get any further comment, please reopen or open another one.

@parroty parroty closed this as completed Feb 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants