{"id":2281,"date":"2019-05-19T19:38:01","date_gmt":"2019-05-19T18:38:01","guid":{"rendered":"https:\/\/denbeke.be\/blog\/?p=2281"},"modified":"2019-05-19T19:38:02","modified_gmt":"2019-05-19T18:38:02","slug":"http-test-recording-with-go-and-go-vcr","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/","title":{"rendered":"HTTP Test Recording with Go and go-vcr"},"content":{"rendered":"<p>When testing code that interacts with HTTP APIs, it is often cumbersome to test them, and especially to automatically test them with real data as part of a continuous deployment process.<\/p>\n<p>To tackle this, it is useful to record your API requests and responses. This can be done in Go by using <a href=\"https:\/\/github.com\/dnaeon\/go-vcr\">go-vcr<\/a>, which gives you a <code>http.Transport<\/code> you can use in a custom <code>http.Client<\/code>.<\/p>\n<p><em>go-vcr<\/em> uses the concept of a <em>recorder<\/em> and a <em>cassette<\/em>. The <em>recorder<\/em> will save a request-response mapping. This is stored in a <em>cassette<\/em>.<\/p>\n<h3 id=\"creating-http.transport-with-go-vcr\">Creating http.Transport with go-vcr<\/h3>\n<p>Creating a custom <code>http.Transport<\/code> is very straightforward. In fact, the <code>recorder<\/code> struct implements the <code>http.Transport<\/code> interface. So it suffices to create a <code>recorder<\/code>.<br \/>\nIn my example I change the mode of the recorder (recording vs replaying) based on whether the <code>UPDATE<\/code> environment flag is set.<br \/>\nThe cassette is stored in <code>testdata\/go-vcr<\/code>, because <code>testdata<\/code> directories are ignored by Go.<\/p>\n<pre class=\"go\"><code>\/\/ UpdateCassette ENV variable so we know when to update the cassette.\n_, UpdateCassette := os.LookupEnv(\"UPDATE\")\n\nrecorderMode := recorder.ModeReplaying\nif UpdateCassette {\n    recorderMode = recorder.ModeRecording\n}\n\n\/\/ Setup recorder\nr, err := recorder.NewAsMode(\"testdata\/go-vcr\", recorderMode, nil)\nif err != nil {\n    return nil, nil, err\n}<\/code><\/pre>\n<h3 id=\"deleting-sensitive-information\">Deleting Sensitive Information<\/h3>\n<p>Since your tests are version controlled, it is very important to delete all sensitive data from the cassette. This can be done by adding filters to the recorder. Below, I delete the <code>x-auth-token<\/code> and <code>authorization<\/code> headers. But depending on the HTTP API you are testing you might need to delete more sensitive data.<\/p>\n<pre class=\"go\"><code>\/\/ Add a filter which removes Authorization and x-auth-token headers from all requests\nr.AddFilter(func(i *cassette.Interaction) error {\n    delete(i.Request.Headers, \"x-auth-token\")\n    delete(i.Request.Headers, \"authorization\")\n    return nil\n})<\/code><\/pre>\n<h3 id=\"doing-http-requests-with-the-custom-transport\">Doing HTTP Requests with the Custom Transport<\/h3>\n<p>Now you have a recording transport, you can start writing tests. First create a HTTP client with the custom transport. Once you have done this, performing HTTP requests and performing tests on them is business as usual\u2026<\/p>\n<pre class=\"go\"><code>\/\/ Create new http.Client where transport is the recorder\nhttpClient := &amp;http.Client{Transport: r}\n\nreq, err := http.NewRequest(\"GET\", \"http:\/\/api.example.com\/some\/object\", nil)\nif err != nil {\n    \/\/ handle error\n}\nresp, err := httpClient.Do(req)\nif err != nil {\n    \/\/ handle error\n}\n\n\/\/ perform tests on the response...<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When testing code that interacts with HTTP APIs, it is often cumbersome to test them, and especially to automatically test them with real data as part of a continuous deployment process. To tackle this, it is useful to record your API requests and responses. This can be done in Go by using go-vcr, which gives [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[235],"tags":[232,278,266,277],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HTTP Test Recording with Go and go-vcr &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTTP Test Recording with Go and go-vcr &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"When testing code that interacts with HTTP APIs, it is often cumbersome to test them, and especially to automatically test them with real data as part of a continuous deployment process. To tackle this, it is useful to record your API requests and responses. This can be done in Go by using go-vcr, which gives [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-19T18:38:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-19T18:38:02+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/\",\"name\":\"HTTP Test Recording with Go and go-vcr &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2019-05-19T18:38:01+00:00\",\"dateModified\":\"2019-05-19T18:38:02+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2281"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=2281"}],"version-history":[{"count":1,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2281\/revisions"}],"predecessor-version":[{"id":2282,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2281\/revisions\/2282"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=2281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=2281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=2281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}