Как вы используете видеомагнитофон и Webmock, чтобы заглушить почтовый запрос с базовой аутентификацией?
Отказ от ответственности: я новичок в API, и я недолго потерян, пытаясь решить эту, казалось бы, простую проблему.
У меня есть код, который взаимодействует с API Github, который уже реализован правильно, но я хочу вернуться и протестировать его с помощью 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
метод - это то, что я хочу проверить, но не могу понять, как имитировать post
атрибут с Webmock и видеомагнитофоном.
Вот как переменная 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's stub_request
и передать его в кассету видеомагнитофона? Я видел много примеров, чтобы заглушить GET
, но я не понимаю, как использовать Webmock для POST
, В частности, мне нужно понять, как вернуть из заглушки Webmock данные, показанные в post
переменная.
Я ищу конкретный пример или полупример, чтобы направить меня в правильном направлении, а не предложение искать какую-то документацию в другом месте, потому что я просто снова буду бегать кругами.
1 ответ
Webmock заглушки запросы с заголовками следующим образом:
stub_request(:post, "www.example.com").with(:body => "abc",
:headers => { 'Content-Length' => 3 })
Читая статью в Википедии, вы можете установить Authorization
Заголовок, как и другие поля заголовка, после небольшой работы по кодированию:
Заголовок авторизации построен следующим образом:[8]
Имя пользователя и пароль объединяются в строку "username:password". Полученная строка затем кодируется с использованием варианта Base64 в соответствии с RFC2045-MIME, за исключением того, что не ограничивается 76 символами / строкой. [9] Метод авторизации и пробел, т. Е. "Basic " затем поместите перед закодированной строкой. Например, если пользовательский агент использует "Aladdin" в качестве имени пользователя и "open sesame" в качестве пароля, тогда заголовок формируется следующим образом:
Авторизация: базовая QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Это должно работать на GET и POST.