Documentation
¶
Overview ¶
Package harhar provides a minimal set of methods and structs to enable HAR logging in a go net/http-based application.
Index ¶
- type Client
- func (c *Client) Do(req *http.Request) (*http.Response, error)
- func (c *Client) Get(url string) (*http.Response, error)
- func (c *Client) Head(url string) (*http.Response, error)
- func (c *Client) Post(url string, bodyType string, body io.Reader) (*http.Response, error)
- func (c *Client) PostForm(url string, data url.Values) (*http.Response, error)
- func (c *Client) WriteLog(filename string) (int, error)
- type ClientInterface
- type Cookie
- type Entry
- type NameValuePair
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// Creator describes the source of the logged requests/responses.
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"`
} `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"`
// contains filtered or unexported fields
}
Client embeds a http.Client and wraps its methods to perform transparent HAR logging for every request and response. It contains the properties for the root "log" node of the HAR, with Creator, Version, and Comment strings.
func (*Client) Do ¶
Do works by calling http.Client.Do on the wrapped client instance. However, it also tracks the request start and end times, and parses elements from the request and response data into HAR log Entries.
func (*Client) Head ¶
Head works just like http.Client.Head, creating a HEAD Request and calling Do.
func (*Client) Post ¶
Post works just like http.Client.Post, creating a POST Request with the provided body data, setting the content-type to bodyType, and calling Do.
type ClientInterface ¶
type ClientInterface interface {
Get(url string) (*http.Response, error)
Head(url string) (*http.Response, error)
Post(url string, bodyType string, body io.Reader) (*http.Response, error)
PostForm(url string, data url.Values) (*http.Response, error)
Do(req *http.Request) (*http.Response, error)
}
ClientInterface allows you to dynamically swap in a harhar.Client for a http.Client if needed. (Although you'll still need to type-convert to use http.Client fields or WriteLog)
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 NameValuePair ¶
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"`
}