deployagram-webmock 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/README.md +9 -0
  4. data/lib/deployagram/webmock.rb +61 -0
  5. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25e495d29ab6919d105a12b93be7d7f023e14fd1e0027b79cd43297c18b97da0
4
+ data.tar.gz: c82c240de2663ae0db03b9ef2d3e2188258ff71e2fb027f75c18106bff04b5ac
5
+ SHA512:
6
+ metadata.gz: 1f14497511e0f1127d18bf6e39a3d465733c31e65c07231e81bfe0e9c8830dab4dc7064e715233c4f97b6a6294c41a7f5839045471a8b42c78af00870a82d0ad
7
+ data.tar.gz: 0bca808bece9a78c99f2777a7c238b0c68032ad477720bd04e3951f1b8d32d1cdc8d6f2b3e850239efc9c1485b511eb00a1eb015e72ccc446de844aad4c6a41e
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ DEPLOYAGRAM PROPRIETARY LICENSE
2
+
3
+ Copyright (c) 2025 Deployagram, Cookiebit and Andy Muir.
4
+
5
+ Permission is hereby granted to use this software solely for its intended purpose.
6
+
7
+ RESTRICTIONS:
8
+ - No modification of the software is permitted
9
+ - No reverse engineering, decompilation, or disassembly
10
+ - No redistribution without explicit written permission
11
+ - Source code remains proprietary and confidential
12
+ - Usage is limited to the compiled npm package (the distributed tarball) artifact only
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # deployagram-webmock
2
+
3
+ Records the HTTP calls that WebMock stubs to the Deployagram Collector.
4
+
5
+ The WebMock equivalent of the Node MSW and Nock recorders. Requires the webmock gem in the test's Gemfile.
6
+
7
+ Depends on: none.
8
+
9
+ Part of the Deployagram Ruby packages. See `../../README.md`.
@@ -0,0 +1,61 @@
1
+ require "json"
2
+ require "time"
3
+ require "webmock"
4
+
5
+ module Deployagram
6
+ # Records the outbound HTTP calls that WebMock stubs, as the Node MSW and Nock recorders do. Each call is logged
7
+ # as a request, then a response, under the caller and callee names. Call Deployagram::WebMock.record(logger, ...)
8
+ # once, after WebMock is loaded.
9
+ module WebMock
10
+ REQUEST_HEADERS_KEY = "Headers".freeze
11
+
12
+ module_function
13
+
14
+ # callees: a Hash from the URL host and port (for example "classicbooks:8080") to the callee's name.
15
+ def record(logger, caller:, callees:)
16
+ ::WebMock.after_request(real_requests_only: false) do |request_signature, response|
17
+ uri = request_signature.uri
18
+ callee = callees["#{uri.host}:#{uri.port}"] || callees[uri.host]
19
+ next unless callee
20
+
21
+ method = request_signature.method.to_s.upcase
22
+ path = request_path(uri).delete_prefix("/")
23
+ logger.log(entry(
24
+ "#{method} Request from #{caller} to #{callee}",
25
+ { REQUEST_HEADERS_KEY => header_lists(request_signature.headers), "Body" => request_signature.body,
26
+ "Endpoint" => path }.to_json,
27
+ caller, callee
28
+ ))
29
+ logger.log(entry(
30
+ "#{method} Response from #{callee} to #{caller}",
31
+ { REQUEST_HEADERS_KEY => header_lists(response.headers), "Body" => response.body,
32
+ "ResponseCode" => Integer(Array(response.status).first) }.to_json,
33
+ callee, caller
34
+ ))
35
+ end
36
+ end
37
+
38
+
39
+ # The Collector reads each header as a list of values, so a single value (WebMock gives a string) is wrapped.
40
+ def header_lists(headers)
41
+ (headers || {}).transform_values { |value| Array(value) }
42
+ end
43
+
44
+ # Addressable::URI, which WebMock uses, has no request_uri, so the path and query are joined here.
45
+ def request_path(uri)
46
+ query = uri.query ? "?#{uri.query}" : ""
47
+ "#{uri.path.to_s.empty? ? "/" : uri.path}#{query}"
48
+ end
49
+
50
+ def entry(message, body, source, target)
51
+ {
52
+ "timestamp" => Time.now.utc.iso8601(6),
53
+ "protocol" => "HTTP",
54
+ "source" => source,
55
+ "target" => target,
56
+ "message" => message,
57
+ "body" => body,
58
+ }
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deployagram-webmock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Deployagram
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: The WebMock equivalent of the Node MSW and Nock recorders. Requires the webmock gem in the test's Gemfile.
13
+ email:
14
+ - andy@deployagram.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/deployagram/webmock.rb
22
+ licenses:
23
+ - SEE LICENSE IN LICENSE
24
+ metadata:
25
+ rubygems_mfa_required: "true"
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ -
32
+ - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "3.2"
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ -
38
+ - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ requirements: []
42
+ rubygems_version: 4.1.0
43
+ specification_version: 4
44
+ summary: Records the HTTP calls that WebMock stubs to the Deployagram Collector
45
+ test_files: []