1

Disclaimer: I'm new to APIs and I'm well beyond lost after a week trying to solve this seemingly simple problem.

I have code that interacts with Github's API that is already implemented correctly but I want to go back and test it using RSpec, Webmock and VCR.

Basically, I have a #create method that allows users to create a new repository on Github.

Here is the code for the method:

def self.create(attributes)

post = HTTParty.post(
    'https://api.github.com/user/repos',

    :headers => {
        'User-Agent' => 'Githubrepo',
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
    },

    :basic_auth => {
        :username => attributes[:username],
        :password => attributes[:password]
    },

    :body => {
        'name' => attributes[:repository],
        'description' => attributes[:description]
    }.to_json
)
Githubrepo.parse_response_from(post, attributes[:wants_ssh])
end

All this is doing is making the actual POST request with certain attributes given and storing that posted information to a variable called post which is then passed as an attribute to another method #parse_response_from. This #parse_response_from method is what I want to test, but I can't understand for the life of me how to mimic the post attribute with Webmock and VCR.

This is what the post variable looks like in production that I need to mimic:

{"id"=>23100237,
 "name"=>"test_repository",
 "full_name"=>"willia15/test_repository",
 "owner"=>
  {"login"=>"willia15",
   "id"=>4252986,
   "avatar_url"=>"https://avatars.githubusercontent.com/u/4252986?v=2",
   "gravatar_id"=>"a64a6453a1d8e9119da938630a8cca33",
   "url"=>"https://api.github.com/users/willia15",
   "html_url"=>"https://github.com/willia15",
   "followers_url"=>"https://api.github.com/users/willia15/followers",
   "following_url"=>
    "https://api.github.com/users/willia15/following{/other_user}",
   "gists_url"=>"https://api.github.com/users/willia15/gists{/gist_id}",
   "starred_url"=>
    "https://api.github.com/users/willia15/starred{/owner}{/repo}",
   "subscriptions_url"=>"https://api.github.com/users/willia15/subscriptions",
   "organizations_url"=>"https://api.github.com/users/willia15/orgs",
   "repos_url"=>"https://api.github.com/users/willia15/repos",
   "events_url"=>"https://api.github.com/users/willia15/events{/privacy}",
   "received_events_url"=>
    "https://api.github.com/users/willia15/received_events",
   "type"=>"User",
   "site_admin"=>false},
 "private"=>false,
 "html_url"=>"https://github.com/willia15/test_repository",
 "description"=>nil,
 "fork"=>false,
 "url"=>"https://api.github.com/repos/willia15/test_repository",
 "forks_url"=>"https://api.github.com/repos/willia15/test_repository/forks",
 "keys_url"=>"https://api.github.com/repos/willia15/test_repository/keys{/key_id}",
 "collaborators_url"=>
  "https://api.github.com/repos/willia15/test_repository/collaborators{/collaborator}",
 "teams_url"=>"https://api.github.com/repos/willia15/test_repository/teams",
 "hooks_url"=>"https://api.github.com/repos/willia15/test_repository/hooks",
 "issue_events_url"=>
  "https://api.github.com/repos/willia15/test_repository/issues/events{/number}",
 "events_url"=>"https://api.github.com/repos/willia15/test_repository/events",
 "assignees_url"=>
  "https://api.github.com/repos/willia15/test_repository/assignees{/user}",
 "branches_url"=>
  "https://api.github.com/repos/willia15/test_repository/branches{/branch}",
 "tags_url"=>"https://api.github.com/repos/willia15/test_repository/tags",
 "blobs_url"=>"https://api.github.com/repos/willia15/test_repository/git/blobs{/sha}",
 "git_tags_url"=>
  "https://api.github.com/repos/willia15/test_repository/git/tags{/sha}",
 "git_refs_url"=>
  "https://api.github.com/repos/willia15/test_repository/git/refs{/sha}",
 "trees_url"=>"https://api.github.com/repos/willia15/test_repository/git/trees{/sha}",
 "statuses_url"=>
  "https://api.github.com/repos/willia15/test_repository/statuses/{sha}",
 "languages_url"=>"https://api.github.com/repos/willia15/test_repository/languages",
 "stargazers_url"=>"https://api.github.com/repos/willia15/test_repository/stargazers",
 "contributors_url"=>
  "https://api.github.com/repos/willia15/test_repository/contributors",
 "subscribers_url"=>
  "https://api.github.com/repos/willia15/test_repository/subscribers",
 "subscription_url"=>
  "https://api.github.com/repos/willia15/test_repository/subscription",
 "commits_url"=>"https://api.github.com/repos/willia15/test_repository/commits{/sha}",
 "git_commits_url"=>
  "https://api.github.com/repos/willia15/test_repository/git/commits{/sha}",
 "comments_url"=>
  "https://api.github.com/repos/willia15/test_repository/comments{/number}",
 "issue_comment_url"=>
  "https://api.github.com/repos/willia15/test_repository/issues/comments/{number}",
 "contents_url"=>
  "https://api.github.com/repos/willia15/test_repository/contents/{+path}",
 "compare_url"=>
  "https://api.github.com/repos/willia15/test_repository/compare/{base}...{head}",
 "merges_url"=>"https://api.github.com/repos/willia15/test_repository/merges",
 "archive_url"=>
  "https://api.github.com/repos/willia15/test_repository/{archive_format}{/ref}",
 "downloads_url"=>"https://api.github.com/repos/willia15/test_repository/downloads",
 "issues_url"=>
  "https://api.github.com/repos/willia15/test_repository/issues{/number}",
 "pulls_url"=>"https://api.github.com/repos/willia15/test_repository/pulls{/number}",
 "milestones_url"=>
  "https://api.github.com/repos/willia15/test_repository/milestones{/number}",
 "notifications_url"=>
  "https://api.github.com/repos/willia15/test_repository/notifications{?since,all,participating}",
 "labels_url"=>"https://api.github.com/repos/willia15/test_repository/labels{/name}",
 "releases_url"=>
  "https://api.github.com/repos/willia15/test_repository/releases{/id}",
 "created_at"=>"2014-08-19T06:59:50Z",
 "updated_at"=>"2014-08-19T06:59:50Z",
 "pushed_at"=>"2014-08-19T06:59:50Z",
 "git_url"=>"git://github.com/willia15/test_repository.git",
 "ssh_url"=>"git@github.com:willia15/test_repository.git",
 "clone_url"=>"https://github.com/willia15/test_repository.git",
 "svn_url"=>"https://github.com/willia15/test_repository",
 "homepage"=>nil,
 "size"=>0,
 "stargazers_count"=>0,
 "watchers_count"=>0,
 "language"=>nil,
 "has_issues"=>true,
 "has_downloads"=>true,
 "has_wiki"=>true,
 "forks_count"=>0,
 "mirror_url"=>nil,
 "open_issues_count"=>0,
 "forks"=>0,
 "open_issues"=>0,
 "watchers"=>0,
 "default_branch"=>"master",
 "permissions"=>{"admin"=>true, "push"=>true, "pull"=>true},
 "network_count"=>0,
 "subscribers_count"=>1}

Basically, all I really need from this hash is a few variables to pass into the #parse_response_from method. The way I'm testing this now is just storing certain static key/value pairs into a before block and testing that code. Here's a look:

before(:each) do
    @api_data = { "name" => "test_repo",
                  "ssh_url" => "git@github.com:test_user/test_repo.git",
                  "clone_url" => "https://github.com/test_user/test_repo.git" 
                }
end

How do I stub this with using Webmock's stub_request and pass it into a VCR cassette? I've seen a lot of examples for stubbing a GET, but I don't understand how to use Webmock for POST. Specifically, I need to understand how to return from a Webmock stub the data shown in the post variable.

I'm looking for a specific example or semi-example to guide me in the right direction, not a suggestion on looking elsewhere at some documentation because I'll just be running in circles again.

Aaron
  • 6,466
  • 7
  • 37
  • 75

1 Answers1

1

Webmock stubs requests with headers thusly:

stub_request(:post, "www.example.com").with(:body => "abc",
  :headers => { 'Content-Length' => 3 })

It seems from reading the Wikipedia article that you can set the Authorization header like the other header fields, after a little encoding work:

The Authorization header is constructed as follows:[8]

Username and password are combined into a string "username:password" The resulting string is then encoded using the RFC2045-MIME variant of Base64, except not limited to 76 char/line[9] The authorization method and a space i.e. "Basic " is then put before the encoded string. For example, if the user agent uses 'Aladdin' as the username and 'open sesame' as the password then the header is formed as follows:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

That ought to work for GET and POST.

zetetic
  • 47,184
  • 10
  • 111
  • 119