Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active August 14, 2020 16:14
Show Gist options
  • Save podhmo/d15f7e47ae830a0352159159d7864a02 to your computer and use it in GitHub Desktop.
Save podhmo/d15f7e47ae830a0352159159d7864a02 to your computer and use it in GitHub Desktop.
---
version: 1
interactions:
- request:
body: ""
form: {}
headers: {}
url: https://httpbin.org/headers
method: GET
response:
body: "{\n \"headers\": {\n \"Accept-Encoding\": \"gzip\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"Go-http-client/2.0\", \n \"X-Amzn-Trace-Id\": \"Root=1-5f36b69c-c5b786ff21a06da51a757764\"\n }\n}\n"
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Origin:
- '*'
Content-Length:
- "190"
Content-Type:
- application/json
Date:
- Fri, 14 Aug 2020 16:06:52 GMT
Server:
- gunicorn/19.9.0
status: 200 OK
code: 200
duration: ""
package main
import (
"io"
"log"
"net/http"
"os"
"time"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
)
func main() {
if err := run(); err != nil {
log.Fatalf("!%+v", err)
}
}
func run() error {
r, err := recorder.New("fixtures/01")
if err != nil {
return err
}
defer r.Stop()
// 此処は省略可能
{
r.AddFilter(func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")
return nil
})
r.AddPassthrough(func(req *http.Request) bool {
return req.URL.Path == "/login"
})
}
client := &http.Client{
Transport: r,
Timeout: 3 * time.Second,
}
url := os.Getenv("URL")
res, err := client.Get(url)
if err != nil {
return err
}
if _, err := io.Copy(os.Stdout, res.Body); err != nil {
return err
}
defer res.Body.Close()
return nil
}
export URL ?= https://httpbin.org/headers
01:
go run main.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment