harhar

package module
v0.0.0-...-acb91b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2016 License: MIT Imports: 6 Imported by: 2

README

harhar

HTTP Archive (HAR) recording for Go code using the http.RoundTripper interface.

Getting Started

For logging from an http.Client you can simply set the Transport property:

	recorder := harhar.NewRecorder(http.DefaultTransport) 
	client := &http.Client{
		Transport: recorder,
	}

Then, whenever you're ready to generate the HAR output, call WriteFile:

recorder.WriteFile("output.har")

Documentation

Overview

Package harhar provides a minimal set of methods and structs to enable HAR logging in a go net/http-based application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cookie struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Path     string `json:"path,omitempty"`
	Domain   string `json:"domain,omitempty"`
	Expires  string `json:"expires,omitempty"` // ISO8601 time
	Secure   bool   `json:"secure"`
	HttpOnly bool   `json:"httpOnly"`
}

type Creator

type Creator struct {
	// Name defaults to the name of the program (os.Args[0])
	Name string `json:"name"`

	// Version defaults to the current time (formatted as "20060102150405")
	Version string `json:"version"`
}

Creator describes the source of the logged requests/responses.

type Entry

type Entry struct {
	Request  Request  `json:"request"`
	Response Response `json:"response"`

	Start string `json:"startedDateTime"` // ISO8601 time

	// Total time in milliseconds, Time=SUM(Timings.*)
	Time    int `json:"time"`
	Timings struct {
		Send    int `json:"send"`
		Wait    int `json:"wait"`
		Receive int `json:"receive"`
	} `json:"timings"`

	// always empty
	Cache map[string]string `json:"cache"`
}

type HAR

type HAR struct {
	Log Log `json:"log"`
}

func NewHAR

func NewHAR() *HAR

type Log

type Log struct {
	Creator Creator `json:"creator"`

	// Version defaults to the current time (formatted as "20060102150405")
	Version string `json:"version"`

	// Comment can be added to the log to describe the particulars of this data.
	Comment string `json:"comment,omitempty"`

	// Entries contains all of the Request and Response details that passed
	// through this Client.
	Entries []Entry `json:"entries"`
}

type NameValuePair

type NameValuePair struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type Recorder

type Recorder struct {
	http.RoundTripper `json:"-"`
	HAR               *HAR
}

Client embeds an upstream RoundTripper and wraps its methods to perform transparent HAR logging for every request and response

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns a new Recorder object that fulfills the http.RoundTripper interface

func (*Recorder) RoundTrip

func (c *Recorder) RoundTrip(req *http.Request) (*http.Response, error)

func (*Recorder) WriteFile

func (c *Recorder) WriteFile(filename string) (int, error)

WriteLog writes the HAR log format to the filename given, then returns the number of bytes.

type Request

type Request struct {
	Method      string          `json:"method"` // in caps, GET/POST/etc
	URL         string          `json:"url"`
	HttpVersion string          `json:"httpVersion"` // ex "HTTP/1.1"
	Headers     []NameValuePair `json:"headers"`
	Cookies     []Cookie        `json:"cookies"`
	QueryParams []NameValuePair `json:"queryString"`

	Body struct {
		MIMEType string `json:"mimeType"`
		Content  string `json:"text"`
	} `json:"postData"`

	// always -1, too lazy
	HeadersSize int `json:"headersSize"`
	BodySize    int `json:"bodySize"`
}

type Response

type Response struct {
	StatusCode  int             `json:"status"`      // 200
	StatusText  string          `json:"statusText"`  // "OK"
	HttpVersion string          `json:"httpVersion"` // ex "HTTP/1.1"
	RedirectURL string          `json:"redirectURL"`
	Headers     []NameValuePair `json:"headers"`
	Cookies     []Cookie        `json:"cookies"`

	Body struct {
		Size     int    `json:"size"`
		MIMEType string `json:"mimeType"`
		Content  string `json:"text"`
	} `json:"content"`

	// always -1, too lazy
	HeadersSize int `json:"headersSize"`
	BodySize    int `json:"bodySize"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL