title: Mocking HTTPS responses in Go
body: <p>I'm trying to write tests for a package that makes requests to a web service.  I'm running into issues probably due to my lack of understanding of TLS.</p>
  
  <p>Currently my test looks something like this:</p>
  
  <pre><code>func TestSimple() {
      server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
          w.Header().Set("Content-Type", "application/json")
          w.WriteHeader(200)
          fmt.Fprintf(w, `{ "fake" : "json data here" }`)
      }))
      transport := &amp;http.Transport{
          Proxy: func(req *http.Request) (*url.URL, error) {
              return url.Parse(server.URL)
          },
      }
      // Client is the type in my package that makes requests
      client := Client{
          c: http.Client{Transport: transport},
      }
  
      client.DoRequest() // ...
  }
  </code></pre>
  
  <p>My package has a package variable (I'd like for it to be a constant..) for the base address of the web service to query.  It is an https URL.  The test server I created above is plain HTTP, no TLS.  </p>
  
  <p>By default, my test fails with the error "tls: first record does not look like a TLS handshake."</p>
  
  <p>To get this to work, my tests change the package variable to a plain http URL instead of https before making the query.</p>
  
  <p>Is there any way around this?  Can I make the package variable a constant (https), and either set up a <code>http.Transport</code> that "downgrades" to unencrypted HTTP, or use <code>httptest.NewTLSServer()</code> instead?  </p>
  
  <p>(When I try to use <code>NewTLSServer()</code> I get "http: TLS handshake error from 127.0.0.1:45678: tls: oversized record received with length 20037")</p>