如何使用VCR和Webmock使用基本身份验证来存根发布请求?

时间:2014-08-19 17:40:55

标签: ruby api rspec vcr webmock

免责声明:我是API的新手,经过一周的努力解决这个看似简单的问题,我已经远远超过了。

我有与Github的API交互的代码已经正确实现但我想回去使用RSpec,Webmock和VCR进行测试。

基本上,我有一个#create方法,允许用户在Github上创建一个新的存储库。

以下是该方法的代码:

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

所有这一切都是在给出具有某些属性的实际POST请求并将发布的信息存储到名为post的变量中,然后将其作为属性传递给另一个方法#parse_response_from 。这个#parse_response_from方法是我想要测试的方法,但我无法理解我的生活如何用Webmock和VCR模仿post属性。

这就是我需要模仿生产中的post变量:

{"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}

基本上,我真正需要的是这个散列中的一些变量要传递给#parse_response_from方法。我现在测试它的方法是将某些静态键/值对存储到before块并测试该代码。这是一个看:

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

如何使用Webmock的stub_request将其存根并将其传递到VCR卡带?我已经看到很多用于存储GET的示例,但我不明白如何将{Web引用}用于POST。具体来说,我需要了解如何从Webmock存根返回post变量中显示的数据。

我正在寻找一个具体的示例或半示例来指导我朝着正确的方向前进,而不是寻找其他地方某些文档的建议,因为我将再次在圈子中运行。

1 个答案:

答案 0 :(得分:1)

Webmock通过标头存储请求:

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

通过阅读Wikipedia article,您可以在进行一些编码工作后设置Authorization标头,就像其他标头字段一样:

  

Authorization标头的构造如下:[8]

     

用户名和密码组合成一个字符串“username:password”   然后使用RFC2045-MIME变体对结果字符串进行编码   Base64,但不限于76 char / line [9]授权方法   然后在编码的字符串之前放置一个空格,即“Basic”。对于   例如,如果用户代理使用“Aladdin”作为用户名并且“打开”   sesame'作为密码,然后标题形成如下:

     

授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

这应该适用于GET和POST。