免责声明:我是api新手,花了一周时间试图解决这个看似简单的问题,我已经完全迷失了方向。
我有代码与Github的API交互,已经正确实现,但我想回去使用RSpec, Webmock和VCR进行测试。
基本上,我有一个#create
方法,允许用户在Github上创建一个新的存储库。
def self.create(属性)
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
的例子,但是我不明白如何为POST
使用Webmock。具体来说,我需要了解如何从Webmock存根返回post
变量中显示的数据。
我正在寻找一个特定的例子或半例子来指导我在正确的方向上,而不是在其他地方寻找一些文档的建议,因为我只是在兜圈子。
Webmock存根请求这样:
stub_request(:post, "www.example.com").with(:body => "abc",
:headers => { 'Content-Length' => 3 })
似乎从阅读维基百科的文章,你可以设置Authorization
头像其他头字段,经过一点编码工作:
授权头的构造如下:[8]
用户名和密码组合成字符串" Username:password"的RFC2045-MIME变体对生成的字符串进行编码Base64,除不限于76 char/line外[9]的授权方法一个空间是。然后将"Basic"放在编码字符串之前。为例如,如果用户代理使用'Aladdin'作为用户名和'open '
授权:Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==