Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/har/net/http.rb

Overview

Reopen and monkey-patch the request method to capture traffic

Instance Method Summary collapse

Instance Method Details

#original_requestObject



19
# File 'lib/har/net/http.rb', line 19

alias original_request request

#request(req, body = nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/har/net/http.rb', line 21

def request(req, body = nil, &block)
  start_time = Time.now
  res = original_request(req, body, &block)
  end_time = Time.now
  duration = ((end_time.to_f - start_time.to_f) * 1000).round

  har_entry = {
    # https://gist.github.com/dstagner/193207fed46acf5b5bae
    startedDateTime: start_time.round(10).iso8601(6),
    time: duration,
    request: {
      method: req.method,
      url: req.uri.to_s,
      httpVersion: "HTTP/1.1",
      cookies: [],
      headers: req.to_hash.map { |name, value| { name: name, value: value, comment: "" } },
      queryString: [],
      headersSize: -1,
      bodySize: -1
    },
    response: {
      status: res.code,
      statusText: res.message,
      httpVersion: res.http_version,
      cookies: [],
      headers: [],
      content: {},
      redirectURL: "",
      headersSize: 160,
      bodySize: 850,
      comment: ""
    },
    cache: {},  # Not applicable to server-side logging.
    timings: {
      send: 0,  # Mandatory, but not applicable to server-side logging.
      wait: duration,
      receive: 0  # Mandatory, but not applicable to server-side logging.
    }
  }
  har = ::Har::Net::HTTP.har ||= {
    log: {
      version: "1.2",
      creator: {
        name: "har-net-http",
        version: ::Har::Net::HTTP::VERSION,
        comment: ""
      },
      browser: {
        name: RUBY_ENGINE,
        version: RUBY_VERSION,
        comment: ""
      },
      pages: [],
      entries: [],
      comment: ""
    }
  }
  har[:log][:entries] << har_entry

  res
end