{"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":{"footnotes":""},"categories":[235],"tags":[232,278,266,277],"class_list":["post-2281","post","type-post","status-publish","format-standard","hentry","category-programming","tag-golang","tag-http","tag-programming","tag-test","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>HTTP Test Recording with Go and go-vcr &#8211; 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 &#8211; 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=\"author\" content=\"Mathias Beke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mathias Beke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/\"},\"author\":{\"name\":\"Mathias Beke\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"headline\":\"HTTP Test Recording with Go and go-vcr\",\"datePublished\":\"2019-05-19T18:38:01+00:00\",\"dateModified\":\"2019-05-19T18:38:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/\"},\"wordCount\":247,\"commentCount\":0,\"keywords\":[\"golang\",\"http\",\"programming\",\"test\"],\"articleSection\":[\"programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/\",\"name\":\"HTTP Test Recording with Go and go-vcr &#8211; 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\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programming\\\/http-test-recording-with-go-and-go-vcr\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HTTP Test Recording with Go and go-vcr\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HTTP Test Recording with Go and go-vcr &#8211; DenBeke","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/","og_locale":"en_US","og_type":"article","og_title":"HTTP Test Recording with Go and go-vcr &#8211; DenBeke","og_description":"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;]","og_url":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/","og_site_name":"DenBeke","article_published_time":"2019-05-19T18:38:01+00:00","article_modified_time":"2019-05-19T18:38:02+00:00","author":"Mathias Beke","twitter_card":"summary_large_image","twitter_creator":"@MthsBk","twitter_site":"@MthsBk","twitter_misc":{"Written by":"Mathias Beke","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/#article","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/"},"author":{"name":"Mathias Beke","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"headline":"HTTP Test Recording with Go and go-vcr","datePublished":"2019-05-19T18:38:01+00:00","dateModified":"2019-05-19T18:38:02+00:00","mainEntityOfPage":{"@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/"},"wordCount":247,"commentCount":0,"keywords":["golang","http","programming","test"],"articleSection":["programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/","url":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/","name":"HTTP Test Recording with Go and go-vcr &#8211; 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"},"breadcrumb":{"@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/denbeke.be\/blog\/programming\/http-test-recording-with-go-and-go-vcr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/denbeke.be\/blog\/"},{"@type":"ListItem","position":2,"name":"HTTP Test Recording with Go and go-vcr"}]},{"@type":"WebSite","@id":"https:\/\/denbeke.be\/blog\/#website","url":"https:\/\/denbeke.be\/blog\/","name":"DenBeke","description":"Mathias Beke","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/denbeke.be\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc","name":"Mathias Beke","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","caption":"Mathias Beke"}}]}},"_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/2281","targetHints":{"allow":["GET"]}}],"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}]}}