Skip to content

Instantly share code, notes, and snippets.

@ecarnevale
Created May 16, 2017 10:41
Show Gist options
  • Save ecarnevale/3a3a10b5bbae694d725de5a956d04d81 to your computer and use it in GitHub Desktop.
Save ecarnevale/3a3a10b5bbae694d725de5a956d04d81 to your computer and use it in GitHub Desktop.
Code snippets for a blog post on ExVCR
# test/support/api_client.exs
defmodule ExVCR.Adapter.HackneyTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
setup_all do
HTTPoison.start
end
end
# test/support/api_client.exs
defmodule ExVCR.Adapter.HackneyTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
setup_all do
HTTPoison.start
end
test "successful login request" do
use_cassette "api_client/login" do
username = System.get_env("API_USERNAME")
password = System.get_env("API_PASSWORD")
response = HTTPoison.get! "https://example.com/api/login", [], [hackney: [basic_auth: {username, password}]]
assert response != nil
assert response.status_code == 200
body = Poison.decode!(response.body, keys: :atoms!)
assert body[:res] == "OK"
cookie = for {"Set-Cookie", cookie} <- response.headers, do: cookie
assert cookie != nil
end
end
end
# exvcr cassettes with censored sensitive data
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": {
"basic_auth": "***"
},
"request_body": "",
"url": "https://example.com/api/login"
},
"response": {
"body": "{\"res\":\"OK\"}",
"headers": {
"Cache-Control": "max-age=0, private, must-revalidate",
"Content-Type": "application/json; charset=utf-8",
"Date": "Tue, 16 May 2017 10:29:34 GMT",
"ETag": "W/\"df23acc5b53ffca3961972019eefa517\"",
"Server": "Apache",
"Set-Cookie": "<<access_key>>",
"Status": "200 OK",
"Vary": "Accept-Encoding",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "1; mode=block",
"transfer-encoding": "chunked",
"Connection": "keep-alive"
},
"status_code": 200,
"type": "ok"
}
}
]
# exvcr cassette with fake sensitive data
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": {
"basic_auth": {
"username",
"password"
}
},
"request_body": "",
"url": "https://example.com/api/login"
},
"response": {
"body": "{\"res\":\"OK\"}",
"headers": {
"Cache-Control": "max-age=0, private, must-revalidate",
"Content-Type": "application/json; charset=utf-8",
"Date": "Tue, 16 May 2017 10:01:16 GMT",
"ETag": "W/\"df23acc5b53ffca3961972019eefa517\"",
"Server": "Apache",
"Set-Cookie": "_example_session_v6=COOKIESTRING; path=/; HttpOnly",
"Status": "200 OK",
"Vary": "Accept-Encoding",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "SAMEORIGIN",
"X-Request-Id": "00000000-0000-0000-0000-000000000000",
"X-XSS-Protection": "1; mode=block",
"Content-Length": "12",
"Connection": "keep-alive"
},
"status_code": 200,
"type": "ok"
}
}
]
# config/mix.exs
defp deps do
{:httpoison, "~> 0.11.0"},
{:exvcr, "~> 0.8", only: :test}
end
# config/test.exs
config :exvcr, [
filter_sensitive_data: [
[pattern: "_example_session_.*", placeholder: "<<access_key>>"]
],
filter_request_options: ["basic_auth"]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment