Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

registered request stub is missing `to_return` if request body has an actual xml value #209

Closed
cecillem opened this Issue · 9 comments

3 participants

@cecillem

I am registering my request as follows:

url = "http://www.example.com/1"
stub_request(:get, url).
   with(body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n    <id>1</id>\n</project>\n",
   headers: {
       'Accept' => 'application/xml',
       'Content-type' => 'application/xml',
       'User-Agent' => 'Ruby',
       'X-Trackertoken' => '12345'
     }).
   to_return(status: 200, body: '', headers: {})

When I ran bundle exec rspec spec, I get a message that my request isn't stub yet and that the registered stub is:

stub_request(:get, "http://www.example.com/1").
  with(body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n    <id>1</id>\n</project>\n",
    headers: {
      'Accept' => 'application/xml',
      'Content-type' => 'application/xml',
      'User-Agent' => 'Ruby',
      'X-Trackertoken' => '12345'
    })

which is almost similar to what I registered except that it is missing the to_return values.

Am I the only one experiencing this? Am I using it incorrectly?

Please enlighten me.

Thanks.

@bblimke
Owner

What http client gem do you use?

Can you please provide a sample code stub declaration + executed request?

@fluxsaas

i think i have the same problem. I'm working on a project with some friends, it runs everywhere but not on my mashine.

i get this running the specs:

e.message = "Real HTTP connections are disabled. Unregistered request: PUT https://*****:@api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96 with body '<subscription>  <plan_code>plus_1</plan_code>  <timeframe>now</timeframe></subscription>' with headers {'Accept'=>'application/xml', 'Content-Type'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'}

You can stub this request with the following snippet:

stub_request(:put, "https://*****:@api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96").
  with(:body => "<subscription>  <plan_code>plus_1</plan_code>  <timeframe>now</timeframe></subscription>",
       :headers => {'Accept'=>'application/xml', 'Content-Type'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'}).
  to_return(:status => 200, :body => "", :headers => {})

registered request stubs:

stub_request(:get, "https://*****:@api.recurly.com/v2/accounts/1/invoices").
  with(:headers => {'Accept'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'})
stub_request(:get, "https://*****:@api.recurly.com/v2/accounts/1/subscriptions").
  with(:headers => {'Accept'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'})
stub_request(:put, "https://*****:@api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96").
  with(:body => "<subscription><plan_code>plus_1</plan_code><timeframe>now</timeframe></subscription>",
       :headers => {'Accept'=>'application/xml', 'Content-Type'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'})
stub_request(:get, "https://*****:@api.recurly.com/v2/accounts/1").
  with(:headers => {'Accept'=>'application/xml', 'User-Agent'=>'Recurly/2.1.5; ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.3.0]'})

stub definition:

module RecurlyMacros
  USER_AGENT_STRING = "Recurly/2.1.5; #{RUBY_DESCRIPTION}"
  def account_subscribe(plan, timeframe)
    WebMock.stub_request(:put, "https://#{AppConfig.recurly['api_key']}:@api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96").
      with(:headers => {'Accept'=>'application/xml', 'Content-Type'=>'application/xml' ,'User-Agent'=> USER_AGENT_STRING}, 
           :body => "<subscription><plan_code>#{plan.code}</plan_code><timeframe>#{timeframe}</timeframe></subscription>").
      to_return(
        :status => 200, 
        :body => "<?xml version='1.0' encoding='UTF-8'?>
                    <subscription href='https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96'>
                      <account href='https://api.recurly.com/v2/accounts/1'/>
                      <plan href='https://api.recurly.com/v2/plans/#{plan.code}'>
                        <plan_code>#{plan.code}</plan_code>
                        <name>#{plan.name}</name>
                      </plan>
                      <uuid>44f83d7cba354d5b84812419f923ea96</uuid>
                      <state>active</state>
                      <unit_amount_in_cents type='integer'>#{plan.amount}</unit_amount_in_cents>
                      <currency>EUR</currency>
                      <quantity type='integer'>1</quantity>
                      <activated_at type='datetime'>2011-05-27T07:00:00Z</activated_at>
                      <current_period_started_at type='datetime'>2011-06-27T07:00:00Z</current_period_started_at>
                      <current_period_ends_at type='datetime'>2010-07-27T07:00:00Z</current_period_ends_at>
                      <a name='cancel' href='https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/cancel' method='put'/>
                      <a name='terminate' href='https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate' method='put'/>
                    </subscription>", 
        :headers => {})
  end
end
@bblimke
Owner

This example doesn't include the code that executes the request. Stub declarations are not enough. In order to investigate that, I need a sample code/spec: stub declaration + request executed, which I can run to reproduce this issue.

@fluxsaas

well, that's gonna be hard. the request comes from recurly (it's a payment api gem). i gonna write a simple spec case which throws the same error...

@cecillem

Hi bblimke,

Sorry about giving an insufficient problem report. Here's the link to a sample spec: https://gist.github.com/3664330

Thanks.

@bblimke
Owner

Great example, I can reproduce it. Thank you. I'll investigate it when I find a moment.

@bblimke
Owner

@cecillem I'm not sure what you want to achieve in your example but it can't work.

You create a stub with expected request body, but then you make a request without any body specified,
and WebMock correctly doesn't match.
Also, why do you try to make a GET request with body? Shouldn't you be making POST or PUT requests?
GET requests don't accept body.

@cecillem

@bblimke sorry my mistake. I'll add a body to my request and give you an update asap. thanks!

@cecillem

@bblimke you're right. I was stubbing my request the wrong way. I replaced my get request stub with a post request and it worked! It was just my mistake. Thanks. I'm closing this ticket now

@cecillem cecillem closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.