{"id":5383,"date":"2017-09-13T20:45:00","date_gmt":"2017-09-13T20:45:00","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5383"},"modified":"2017-09-13T20:45:00","modified_gmt":"2017-09-13T20:45:00","slug":"recording-http-interactions-with-scotch","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/recording-http-interactions-with-scotch\/","title":{"rendered":"Recording HTTP interactions with Scotch"},"content":{"rendered":"<p>Scotch is an HTTP recorder and playback library. What this means is that, whether we&#8217;re writing unit tests which require an HTTP connection or content, or if we need to test and application offline, we can first record a session, running code against our HttpClient and record the results of any calls, then take our application offline and replay those interactions.<\/p>\n<p>Let&#8217;s get started&#8230;<\/p>\n<p>Add the NuGet package scotch. If you&#8217;re using C# this will also deploy some F# assemblies, don&#8217;t panic, as it&#8217;s .NET it&#8217;s runnable from C# and luckily the API uses C# design style and hence looks like any other C#\/.NET framework library.<\/p>\n<p>This example, shows how we might have code which connects to the <a href=\"https:\/\/github.com\/mleech\/scotch\" target=\"_blank\">scotch GitHub page<\/a> and using ScotchMoe.Recording, we record any interactios to the httpClient and save these to the data.json file &#8211; this example shows using the HttpClients.NewHttpClientWithHandler if we need to go through a proxy server, otherwise we just use HttpClients.NewHttpClient<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar proxy = new WebProxy(proxy, port)\r\n{\r\n   Credentials = CredentialCache.DefaultCredentials\r\n};\r\nvar httpHandler = new HttpClientHandler\r\n{\r\n   Proxy = proxy\r\n};\r\n\r\nvar httpClient = HttpClients.NewHttpClientWithHandler(\r\n   httpHandler, \r\n   @&quot;c:\\Development\\interactions\\data.json&quot;, \r\n   ScotchMode.Recording);\r\n\r\nvar result = await httpClient.GetAsync(\r\n   new Uri(&quot;https:\/\/github.com\/mleech\/scotch&quot;));\r\n<\/pre>\n<p>Now, assuming we&#8217;ve saved a recording we can switch the code to ScotchMode.Replaying (this example demonstrates non-proxy code and hence uses HttpClients.NewHttpClient<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar httpClient = HttpClients.NewHttpClient(\r\n   @&quot;c:\\Development\\interactions\\data.json&quot;, \r\n   ScotchMode.Replaying);\r\n\r\nvar result = await httpClient.GetAsync(\r\n   new Uri(&quot;https:\/\/github.com\/mleech\/scotch&quot;));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Scotch is an HTTP recorder and playback library. What this means is that, whether we&#8217;re writing unit tests which require an HTTP connection or content, or if we need to test and application offline, we can first record a session, running code against our HttpClient and record the results of any calls, then take our [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[183],"tags":[],"class_list":["post-5383","post","type-post","status-publish","format-standard","hentry","category-scotch"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5383"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5383\/revisions"}],"predecessor-version":[{"id":5388,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5383\/revisions\/5388"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}