Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:46:47+00:00 2026-06-10T13:46:47+00:00

I am registering a request stub as follows: url = http://www.example.com/1 stub_request(:get, url). with(body:

  • 0

I am registering a request stub 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: {})

for some reason when I run bundle exec rspec spec, my specs fails saying that the request isn’t registered yet. The registered stub is this,

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'
       })

note that the to_return part is missing

I tried replacing the body header with an empty string, the request stub is registered correctly but then my specs will still fail because they are expecting some value from the body other than the empty string. Thus, it is really important that I assign a value to body.

In my spec I am calling this method:

def find(id)
  require 'net/http'
  http = Net::HTTP.new('www.example.com')
  headers = {
    "X-TrackerToken" => "12345",
    "Accept"         => "application/xml",
    "Content-type"   => "application/xml",
    "User-Agent"     => "Ruby"
  }
  parse(http.request(Net::HTTP::Get.new("/#{id}", headers)).body)
end

Any ideas on why this is happening?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:46:48+00:00Added an answer on June 10, 2026 at 1:46 pm

    The problem is that your stub is matching a GET request with a non-empty body of <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n <id>1</id>\n</project>\n, but when you make the request you’re not including any body, so it doesn’t find the stub.

    I think you’re confused about what body is what here. The body in the with method arguments is the body of the request you are making, not the body of the response. What you probably want is a stub like this:

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

Sidebar

Related Questions

I am trying to fetch the http object content/body via registering a stream listener
Websocket example in netty (examples) has a http request handler which: performs hand shaking
When registering a product, the user can customize the URL of it! As the
I want to get the ip address whoever is registering in my site. How
I'm getting these errors [http://pastebin.com/BEgNh2mm][chef-client/server errors] after successfully bootstrapping a node with chef. The
I have been trying to determine a best case solution for registering a COM
I have a request spec that looks like this: describe Registering from the main
How can I get the user id of the facebook user who is registering
Is registering components on castle windsor thread-safe? I.e., can multiple threads simultaneously register components
When registering an event via addEventListener on an element, then delete that element without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.