DEV Community

Andrey Krivko
Andrey Krivko

Posted on

How to stub an Upload request to Google API using webmock

The google-api-client gem doesn’t return any response for an upload request when the X-Goog-Upload-Status is not set to 'final'.

When you want to stub an API request you usually write code like this:

stub_request(:post, 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send')
  .with(headers: { 'X-Goog-Upload-Header-Content-Type': 'message/rfc822' })
  .to_return(body: upload_response.to_json,
             headers: {
               content_type: 'application/json'
             })

But it does not work for Google Upload API request – it will return nil as a response. In order to fix this, you should add the 'X-Goog-Upload-Status': 'final' to response headers. So the code will look this way:

stub_request(:post, 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send')
  .with(headers: { 'X-Goog-Upload-Header-Content-Type': 'message/rfc822' })
  .to_return(body: upload_response.to_json,
             headers: {
               content_type: 'application/json',
               'X-Goog-Upload-Status': 'final'
             })

Oldest comments (1)

Collapse
 
junki555 profile image
Junya Kitayama

I was having trouble with this problem. Thank you!