httparty_curl 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.
- checksums.yaml +7 -0
- data/README.md +117 -0
- data/lib/httparty_curl/logger.rb +176 -0
- data/lib/httparty_curl/version.rb +6 -0
- data/lib/httparty_curl.rb +48 -0
- data/test/httparty_curl/logger_test.rb +254 -0
- data/test/test_helper.rb +9 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6326e21907094579b265b6058d10bbf324a232af78107014094f4f9302a664f
|
4
|
+
data.tar.gz: 6ea21e2db0aadefeef5814acfcb1a70c88022c2917f00679ec97b958bf378314
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb447d219baa96116854968bdd9b1e7e47733c02548dd4f57a8f1444a403ec12dd559ed9ab0267e1ce722d94a1937c788d94e5c5e64ec4427191165a84c709e5
|
7
|
+
data.tar.gz: 036730dc4ebf953d1aae54c7e7abfa0e1c9e899bdf184e9cd17e5893db0949ce05301b5911d9445bbb35f5e27456da93bdc683bc660e6540f34bc1a1e1c92381
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
|
2
|
+
# HTTPartyCurl::Logger
|
3
|
+
|
4
|
+
`HTTPartyCurl::Logger` is a module that extends HTTParty to log HTTP requests as cURL commands. This is useful for debugging and inspecting outgoing requests in a readable format, replicating them in a terminal if needed.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
- Logs HTTP requests made using HTTParty as cURL commands.
|
9
|
+
- Supports common HTTP methods: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`.
|
10
|
+
- Automatically includes headers, query parameters, authentication, and proxy settings in the cURL command.
|
11
|
+
- Customizable logging configuration.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'httparty_curl'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
bundle install
|
25
|
+
```
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
gem install httparty_curl
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
To use `HTTPartyCurl::Logger`, simply include it in your class that uses HTTParty _after_ you include `Http:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'httparty_curl/logger'
|
39
|
+
|
40
|
+
class MyApiClient
|
41
|
+
include HTTParty
|
42
|
+
include HTTPartyCurl::Logger
|
43
|
+
|
44
|
+
base_uri 'https://api.example.com'
|
45
|
+
|
46
|
+
def fetch_data
|
47
|
+
self.class.get('/data', headers: { 'Authorization' => 'Bearer token' })
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Now, every request will be logged as a cURL command if logging is enabled.
|
53
|
+
|
54
|
+
### Enabling Logging
|
55
|
+
|
56
|
+
To enable logging, you need to configure the `HTTPartyCurl` logger and set `curl_logging_enabled` to `true`. You can customize the logger based on your logging setup.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
HTTPartyCurl.configure do |config|
|
60
|
+
config.curl_logging_enabled = true
|
61
|
+
config.logger = Logger.new(STDOUT) # or any other logger
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Example Output
|
66
|
+
|
67
|
+
When a request is made, the cURL command will be logged like this:
|
68
|
+
|
69
|
+
```bash
|
70
|
+
HTTParty cURL command:
|
71
|
+
curl -X GET 'https://api.example.com/data' \
|
72
|
+
-H 'Authorization: Bearer token'
|
73
|
+
```
|
74
|
+
|
75
|
+
### Proxy Support
|
76
|
+
|
77
|
+
If you are using proxies, the proxy settings will also be included in the cURL command:
|
78
|
+
|
79
|
+
```bash
|
80
|
+
HTTParty cURL command:
|
81
|
+
curl -X GET 'https://api.example.com/data' \
|
82
|
+
--proxy 'http://proxy_user:proxy_pass@proxy.example.com:8080' \
|
83
|
+
-H 'Authorization: Bearer token'
|
84
|
+
```
|
85
|
+
|
86
|
+
### Authentication
|
87
|
+
|
88
|
+
Basic and Digest authentication methods are also supported and included in the cURL command:
|
89
|
+
|
90
|
+
```bash
|
91
|
+
curl -X GET 'https://api.example.com/data' \
|
92
|
+
-u 'username:password'
|
93
|
+
```
|
94
|
+
|
95
|
+
## Customization
|
96
|
+
|
97
|
+
### Supported HTTP Methods
|
98
|
+
|
99
|
+
By default, the following HTTP methods are overridden with logging:
|
100
|
+
|
101
|
+
- `GET`
|
102
|
+
- `POST`
|
103
|
+
- `PUT`
|
104
|
+
- `PATCH`
|
105
|
+
- `DELETE`
|
106
|
+
|
107
|
+
### Custom Headers, Body, and Query Parameters
|
108
|
+
|
109
|
+
The cURL command generation handles headers, query parameters, and request body data (including multipart forms and JSON payloads).
|
110
|
+
|
111
|
+
## Contributing
|
112
|
+
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thescubageek/httparty_curl.
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# lib/httparty_curl/logger.rb
|
2
|
+
|
3
|
+
module HTTPartyCurl
|
4
|
+
# Module containing the cURL logging functionality.
|
5
|
+
module Logger
|
6
|
+
# List of proxy options to consider.
|
7
|
+
PROXY_OPTIONS = %i[http_proxyaddr http_proxyport http_proxyuser http_proxypass].freeze
|
8
|
+
|
9
|
+
# Hook method called when the module is included.
|
10
|
+
# Extends the base class with class methods.
|
11
|
+
# @param base [Class] the class including the module.
|
12
|
+
def self.included(base)
|
13
|
+
base.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Class methods added to the including class.
|
17
|
+
module ClassMethods
|
18
|
+
# List of HTTP methods to override with logging functionality.
|
19
|
+
HTTP_METHODS = %i[get post put patch delete].freeze
|
20
|
+
|
21
|
+
# Dynamically defines overridden HTTP methods.
|
22
|
+
HTTP_METHODS.each do |http_method|
|
23
|
+
define_method(http_method) do |uri, options = {}, &block|
|
24
|
+
options ||= {}
|
25
|
+
log_curl(http_method, uri, options)
|
26
|
+
super(uri, options, &block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Converts an HTTParty request to a cURL command.
|
31
|
+
#
|
32
|
+
# @param method [Symbol] the HTTP method (e.g., :get, :post).
|
33
|
+
# @param uri [String] the request URI.
|
34
|
+
# @param options [Hash] the request options (headers, body, etc.).
|
35
|
+
# @return [String] the generated cURL command.
|
36
|
+
def to_curl(method, uri, options = {})
|
37
|
+
options ||= {}
|
38
|
+
|
39
|
+
# Prepare the URI and append query parameters if present
|
40
|
+
uri = prepare_uri(uri, options[:query])
|
41
|
+
|
42
|
+
curl_command = initialize_curl_command(method, uri)
|
43
|
+
|
44
|
+
# Add proxy settings if available
|
45
|
+
add_proxy_settings(curl_command)
|
46
|
+
|
47
|
+
# Add headers to the cURL command
|
48
|
+
add_headers(curl_command, options[:headers])
|
49
|
+
|
50
|
+
# Add authentication to the cURL command
|
51
|
+
add_authentication(curl_command, options[:basic_auth], options[:digest_auth])
|
52
|
+
|
53
|
+
# Add body data to the cURL command
|
54
|
+
add_body_data(curl_command, options[:body], options[:headers])
|
55
|
+
|
56
|
+
curl_command.join(" \\\n")
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# Prepares the full URI, including base_uri and query parameters.
|
62
|
+
#
|
63
|
+
# @param uri [String] the request URI.
|
64
|
+
# @param query [Hash, nil] the query parameters.
|
65
|
+
# @return [String] the full URI with query parameters.
|
66
|
+
def prepare_uri(uri, query)
|
67
|
+
# Ensure base_uri is included if it's a relative path
|
68
|
+
unless uri.start_with?('http')
|
69
|
+
effective_base_uri = self.base_uri || 'http://localhost' # rubocop:disable Style/RedundantSelf
|
70
|
+
uri = URI.join(effective_base_uri, uri).to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
# Append query parameters to URI if present
|
74
|
+
if query
|
75
|
+
uri = URI(uri)
|
76
|
+
existing_query = URI.decode_www_form(uri.query || '')
|
77
|
+
new_query = existing_query + query.to_a
|
78
|
+
uri.query = URI.encode_www_form(new_query)
|
79
|
+
uri = uri.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
uri
|
83
|
+
end
|
84
|
+
|
85
|
+
# Initializes the cURL command with the HTTP method and URI.
|
86
|
+
#
|
87
|
+
# @param method [Symbol] the HTTP method.
|
88
|
+
# @param uri [String] the full request URI.
|
89
|
+
# @return [Array<String>] the initial cURL command array.
|
90
|
+
def initialize_curl_command(method, uri)
|
91
|
+
["curl -X #{method.to_s.upcase} '#{uri}'"]
|
92
|
+
end
|
93
|
+
|
94
|
+
# Adds proxy settings to the cURL command if present.
|
95
|
+
#
|
96
|
+
# @param curl_command [Array<String>] the cURL command array.
|
97
|
+
def add_proxy_settings(curl_command)
|
98
|
+
if default_options && default_options.values_at(*PROXY_OPTIONS).any? { |opt| !opt.nil? && !opt.to_s.empty? }
|
99
|
+
proxy_parts = []
|
100
|
+
if default_options[:http_proxyuser] && default_options[:http_proxypass]
|
101
|
+
proxy_parts << "#{default_options[:http_proxyuser]}:#{default_options[:http_proxypass]}@"
|
102
|
+
end
|
103
|
+
proxy_parts << "#{default_options[:http_proxyaddr]}:#{default_options[:http_proxyport]}"
|
104
|
+
curl_command << "--proxy 'http://#{proxy_parts.join}'"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Adds headers to the cURL command.
|
109
|
+
#
|
110
|
+
# @param curl_command [Array<String>] the cURL command array.
|
111
|
+
# @param headers [Hash, nil] the request headers.
|
112
|
+
def add_headers(curl_command, headers)
|
113
|
+
(headers || {}).each do |k, v|
|
114
|
+
curl_command << "-H '#{k}: #{v}'"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Adds authentication options to the cURL command.
|
119
|
+
#
|
120
|
+
# @param curl_command [Array<String>] the cURL command array.
|
121
|
+
# @param basic_auth [Hash, nil] the basic authentication credentials.
|
122
|
+
# @param digest_auth [Hash, nil] the digest authentication credentials.
|
123
|
+
def add_authentication(curl_command, basic_auth, digest_auth)
|
124
|
+
if basic_auth
|
125
|
+
curl_command << "-u '#{basic_auth[:username]}:#{basic_auth[:password]}'"
|
126
|
+
elsif digest_auth
|
127
|
+
curl_command << "--digest -u '#{digest_auth[:username]}:#{digest_auth[:password]}'"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Adds body data to the cURL command.
|
132
|
+
#
|
133
|
+
# @param curl_command [Array<String>] the cURL command array.
|
134
|
+
# @param body [String, Hash, nil] the request body.
|
135
|
+
# @param headers [Hash, nil] the request headers.
|
136
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
137
|
+
def add_body_data(curl_command, body, headers)
|
138
|
+
return if body.nil?
|
139
|
+
|
140
|
+
headers ||= {}
|
141
|
+
content_type = headers['Content-Type'] || headers['content-type']
|
142
|
+
|
143
|
+
if content_type == 'application/x-www-form-urlencoded' && body.is_a?(Hash)
|
144
|
+
form_data = URI.encode_www_form(body)
|
145
|
+
curl_command << "-d '#{form_data}'"
|
146
|
+
elsif content_type == 'multipart/form-data' && body.is_a?(Hash)
|
147
|
+
body.each do |key, value|
|
148
|
+
if value.respond_to?(:path) && value.respond_to?(:read)
|
149
|
+
# File upload
|
150
|
+
curl_command << "-F '#{key}=@#{value.path}'"
|
151
|
+
else
|
152
|
+
curl_command << "-F '#{key}=#{value}'"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
else
|
156
|
+
# Default to JSON encoding for hash bodies
|
157
|
+
body_data = body.is_a?(String) ? body : body.to_json
|
158
|
+
curl_command << "-d '#{body_data}'"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
162
|
+
|
163
|
+
# Logs the cURL command for the request if logging is enabled.
|
164
|
+
#
|
165
|
+
# @param method [Symbol] the HTTP method.
|
166
|
+
# @param uri [String] the request URI.
|
167
|
+
# @param options [Hash] the request options.
|
168
|
+
def log_curl(method, uri, options)
|
169
|
+
return unless HTTPartyCurl.configuration.curl_logging_enabled
|
170
|
+
|
171
|
+
curl_command = to_curl(method, uri, options)
|
172
|
+
HTTPartyCurl.configuration.logger.info("\nHTTParty cURL command:\n#{curl_command}\n")
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# lib/httparty_curl.rb
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require 'httparty_curl/version'
|
5
|
+
require 'logger'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
# Main module for the HTTPartyCurl gem.
|
9
|
+
# Provides functionality to log HTTParty requests as cURL commands.
|
10
|
+
module HTTPartyCurl
|
11
|
+
# Custom error class for the gem.
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
# Configuration class to store gem settings.
|
15
|
+
class Configuration
|
16
|
+
# @return [Boolean] whether cURL logging is enabled.
|
17
|
+
attr_accessor :curl_logging_enabled
|
18
|
+
|
19
|
+
# @return [Logger] the logger instance used for logging.
|
20
|
+
attr_accessor :logger
|
21
|
+
|
22
|
+
# Initializes the configuration with default values.
|
23
|
+
def initialize
|
24
|
+
@curl_logging_enabled = false
|
25
|
+
@logger = ::Logger.new($stdout)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class << self
|
30
|
+
# @return [Configuration] the current configuration instance.
|
31
|
+
attr_accessor :configuration
|
32
|
+
|
33
|
+
# Configures the gem settings.
|
34
|
+
# @yieldparam config [Configuration] the configuration object to set options.
|
35
|
+
# @example
|
36
|
+
# HTTPartyCurl.configure do |config|
|
37
|
+
# config.curl_logging_enabled = true
|
38
|
+
# config.logger = Logger.new('log/httparty_curl.log')
|
39
|
+
# end
|
40
|
+
def configure
|
41
|
+
self.configuration ||= Configuration.new
|
42
|
+
yield(configuration)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Load the Logger module containing the cURL logging functionality.
|
47
|
+
require_relative 'httparty_curl/logger'
|
48
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# test/httparty_curl/logger_test.rb
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'httparty_curl'
|
7
|
+
require 'stringio'
|
8
|
+
require 'webmock/minitest'
|
9
|
+
|
10
|
+
class HTTPartyCurl::LoggerTest < Minitest::Test
|
11
|
+
def setup
|
12
|
+
@base_uri = 'http://example.com'
|
13
|
+
|
14
|
+
# Configure the gem for testing
|
15
|
+
HTTPartyCurl.configure do |config|
|
16
|
+
config.curl_logging_enabled = true
|
17
|
+
config.logger = ::Logger.new(StringIO.new) # Use StringIO to capture logger output
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create a dynamic client class for testing
|
21
|
+
@client_class = Class.new do
|
22
|
+
include HTTParty
|
23
|
+
include HTTPartyCurl::Logger
|
24
|
+
|
25
|
+
base_uri 'http://example.com'
|
26
|
+
end
|
27
|
+
|
28
|
+
# Stub all HTTP requests to example.com
|
29
|
+
stub_request(:any, /example\.com/).to_return(status: 200, body: "", headers: {})
|
30
|
+
|
31
|
+
# Disable real network connections
|
32
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
33
|
+
end
|
34
|
+
|
35
|
+
def teardown
|
36
|
+
# Reset the configuration after each test
|
37
|
+
HTTPartyCurl.configuration = HTTPartyCurl::Configuration.new
|
38
|
+
|
39
|
+
# Reset WebMock after each test
|
40
|
+
WebMock.reset!
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_get_request_logs_curl_command
|
44
|
+
mock_logger = Minitest::Mock.new
|
45
|
+
mock_logger.expect(:info, nil) do |message|
|
46
|
+
message.include?("curl -X GET '#{@base_uri}/'")
|
47
|
+
end
|
48
|
+
|
49
|
+
HTTPartyCurl.configuration.logger = mock_logger
|
50
|
+
|
51
|
+
@client_class.get('/')
|
52
|
+
|
53
|
+
mock_logger.verify
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_post_request_with_body
|
57
|
+
body = { 'key' => 'value' }
|
58
|
+
expected_curl = "curl -X POST '#{@base_uri}/' \\\n-d '#{body.to_json}'"
|
59
|
+
|
60
|
+
captured_output = StringIO.new
|
61
|
+
logger = ::Logger.new(captured_output)
|
62
|
+
# Set a simple formatter to exclude timestamps and severity levels
|
63
|
+
logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
|
64
|
+
HTTPartyCurl.configuration.logger = logger
|
65
|
+
|
66
|
+
@client_class.post('/', body: body)
|
67
|
+
|
68
|
+
assert_includes captured_output.string, expected_curl
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_request_with_headers
|
72
|
+
headers = { 'Content-Type' => 'application/json', 'Authorization' => 'Bearer token' }
|
73
|
+
expected_curl = "-H 'Content-Type: application/json' \\\n-H 'Authorization: Bearer token'"
|
74
|
+
|
75
|
+
captured_output = StringIO.new
|
76
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
77
|
+
|
78
|
+
@client_class.get('/', headers: headers)
|
79
|
+
|
80
|
+
assert_includes captured_output.string, expected_curl
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_request_with_query_params
|
84
|
+
query = { 'param1' => 'value1', 'param2' => 'value2' }
|
85
|
+
expected_uri = "#{@base_uri}/?param1=value1¶m2=value2"
|
86
|
+
|
87
|
+
captured_output = StringIO.new
|
88
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
89
|
+
|
90
|
+
@client_class.get('/', query: query)
|
91
|
+
|
92
|
+
assert_includes captured_output.string, "curl -X GET '#{expected_uri}'"
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_request_with_basic_auth
|
96
|
+
auth = { username: 'user', password: 'pass' }
|
97
|
+
expected_curl = "-u 'user:pass'"
|
98
|
+
|
99
|
+
captured_output = StringIO.new
|
100
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
101
|
+
|
102
|
+
@client_class.get('/', basic_auth: auth)
|
103
|
+
|
104
|
+
assert_includes captured_output.string, expected_curl
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_request_with_digest_auth
|
108
|
+
auth = { username: 'user', password: 'pass' }
|
109
|
+
expected_curl = "--digest -u 'user:pass'"
|
110
|
+
|
111
|
+
captured_output = StringIO.new
|
112
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
113
|
+
|
114
|
+
@client_class.get('/', digest_auth: auth)
|
115
|
+
|
116
|
+
assert_includes captured_output.string, expected_curl
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_post_request_with_form_data
|
120
|
+
body = { 'key1' => 'value1', 'key2' => 'value2' }
|
121
|
+
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
122
|
+
expected_data = "key1=value1&key2=value2"
|
123
|
+
|
124
|
+
captured_output = StringIO.new
|
125
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
126
|
+
|
127
|
+
@client_class.post('/', headers: headers, body: body)
|
128
|
+
|
129
|
+
assert_includes captured_output.string, "-d '#{expected_data}'"
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_post_request_with_multipart_form_data
|
133
|
+
file = File.open(__FILE__)
|
134
|
+
body = { 'file' => file, 'key' => 'value' }
|
135
|
+
headers = { 'Content-Type' => 'multipart/form-data' }
|
136
|
+
|
137
|
+
captured_output = StringIO.new
|
138
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
139
|
+
|
140
|
+
@client_class.post('/', headers: headers, body: body)
|
141
|
+
|
142
|
+
assert_includes captured_output.string, "-F 'file=@#{file.path}'"
|
143
|
+
assert_includes captured_output.string, "-F 'key=value'"
|
144
|
+
|
145
|
+
file.close
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_request_with_proxy_settings
|
149
|
+
@client_class.http_proxy('proxy.example.com', 8080, 'proxyuser', 'proxypass')
|
150
|
+
expected_proxy = "--proxy 'http://proxyuser:proxypass@proxy.example.com:8080'"
|
151
|
+
|
152
|
+
captured_output = StringIO.new
|
153
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
154
|
+
|
155
|
+
@client_class.get('/')
|
156
|
+
|
157
|
+
assert_includes captured_output.string, expected_proxy
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_logging_disabled
|
161
|
+
HTTPartyCurl.configuration.curl_logging_enabled = false
|
162
|
+
mock_logger = Minitest::Mock.new
|
163
|
+
|
164
|
+
HTTPartyCurl.configuration.logger = mock_logger
|
165
|
+
|
166
|
+
@client_class.get('/')
|
167
|
+
|
168
|
+
# If logger.info is called, the mock will raise an error
|
169
|
+
mock_logger.verify
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_custom_logger
|
173
|
+
captured_output = StringIO.new
|
174
|
+
custom_logger = ::Logger.new(captured_output)
|
175
|
+
|
176
|
+
HTTPartyCurl.configuration.logger = custom_logger
|
177
|
+
|
178
|
+
@client_class.get('/')
|
179
|
+
|
180
|
+
assert_includes captured_output.string, "curl -X GET '#{@base_uri}/'"
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_put_request
|
184
|
+
body = { 'update' => 'data' }
|
185
|
+
expected_curl = "curl -X PUT '#{@base_uri}/' \\\n-d '#{body.to_json}'"
|
186
|
+
|
187
|
+
captured_output = StringIO.new
|
188
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
189
|
+
|
190
|
+
@client_class.put('/', body: body)
|
191
|
+
|
192
|
+
assert_includes captured_output.string, expected_curl
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_patch_request
|
196
|
+
body = { 'patch' => 'data' }
|
197
|
+
expected_curl = "curl -X PATCH '#{@base_uri}/' \\\n-d '#{body.to_json}'"
|
198
|
+
|
199
|
+
captured_output = StringIO.new
|
200
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
201
|
+
|
202
|
+
@client_class.patch('/', body: body)
|
203
|
+
|
204
|
+
assert_includes captured_output.string, expected_curl
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_delete_request
|
208
|
+
expected_curl = "curl -X DELETE '#{@base_uri}/'"
|
209
|
+
|
210
|
+
captured_output = StringIO.new
|
211
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
212
|
+
|
213
|
+
@client_class.delete('/')
|
214
|
+
|
215
|
+
assert_includes captured_output.string, expected_curl
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_body_as_string
|
219
|
+
body = '{"raw":"json"}'
|
220
|
+
expected_curl = "-d '#{body}'"
|
221
|
+
|
222
|
+
captured_output = StringIO.new
|
223
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
224
|
+
|
225
|
+
@client_class.post('/', body: body)
|
226
|
+
|
227
|
+
assert_includes captured_output.string, expected_curl
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_headers_case_insensitivity
|
231
|
+
headers = { 'content-type' => 'application/json', 'ACCEPT' => 'application/json' }
|
232
|
+
expected_curl_content_type = "-H 'content-type: application/json'"
|
233
|
+
expected_curl_accept = "-H 'ACCEPT: application/json'"
|
234
|
+
|
235
|
+
captured_output = StringIO.new
|
236
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
237
|
+
|
238
|
+
@client_class.get('/', headers: headers)
|
239
|
+
|
240
|
+
assert_includes captured_output.string, expected_curl_content_type
|
241
|
+
assert_includes captured_output.string, expected_curl_accept
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_handles_nil_options
|
245
|
+
expected_curl = "curl -X GET '#{@base_uri}/'"
|
246
|
+
|
247
|
+
captured_output = StringIO.new
|
248
|
+
HTTPartyCurl.configuration.logger = ::Logger.new(captured_output)
|
249
|
+
|
250
|
+
@client_class.get('/', nil)
|
251
|
+
|
252
|
+
assert_includes captured_output.string, expected_curl
|
253
|
+
end
|
254
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httparty_curl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TheScubaGeek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.22'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.22'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.25'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.25'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: webmock
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.23'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.23'
|
55
|
+
description: HTTPartyCurl adds cURL logging capabilities to HTTParty requests for
|
56
|
+
debugging purposes.
|
57
|
+
email:
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/httparty_curl.rb
|
64
|
+
- lib/httparty_curl/logger.rb
|
65
|
+
- lib/httparty_curl/version.rb
|
66
|
+
- test/httparty_curl/logger_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
homepage: https://github.com/thescubageek/httparty_curl
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 3.0.0
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.5.11
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: A gem to log HTTParty requests as cURL commands.
|
91
|
+
test_files: []
|