How to stub an Upload request to Google API using webmock
Andrey Krivko
Posted on December 25, 2018
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'
})
💖 💪 🙅 🚩
Andrey Krivko
Posted on December 25, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.