api-tester-cli 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 155299a42034d93ed03164056d6ca453bcf3e43acc282ddb015a3b14f4e51b7c
4
+ data.tar.gz: 45e106438c46b807e9493f073f7890779de95f37a8391eb2a390b37c70f6e695
5
+ SHA512:
6
+ metadata.gz: 915563f0c5af3a185729d25bce93d418e76c2842de9412eb24f1cc62fe89f6cbd7e23259b56a75b10bfff026fe0729c1356bc858f23ec9dc01103a193a21ac5b
7
+ data.tar.gz: 3dc5c338d28ede087a7077978e17f8321e680361258c38c83cfe6b69824aba42fc80765a9289acd57d923d0f3867dbe446049423fadc0751f4e65561fb33d654
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+
2
+ # API Tester CLI Tool
3
+ A simple command-line tool to test REST APIs. This tool allows you to interactively input API details or provide them as command-line arguments for quick testing.
4
+
5
+ ## Features
6
+
7
+ - Supports HTTP methods (GET, POST, PUT, DELETE)
8
+ - Allows adding custom headers
9
+ - Supports JSON body for requests
10
+ - Bearer token authentication
11
+ - Interactive mode for beginners
12
+ - Command-line mode for advanced users
13
+ - Colored and formatted output for better readability
14
+
15
+ ## Installation
16
+
17
+ 1. Clone the repository:
18
+ ```bash
19
+ git clone https://github.com/bhavyansh001/api-tester.git
20
+ ```
21
+ 2. Install dependencies:
22
+ ```bash
23
+ bundle install
24
+ ```
25
+ 3. Run the CLI tool:
26
+ ```bash
27
+ cd api-tester
28
+ chmod +x bin/api-tester
29
+ ```
30
+ ## Usage
31
+
32
+ 1. Interactive Mode
33
+ Run the tool without any arguments, and it will prompt you for the necessary information interactively.
34
+ ```bash
35
+ bin/api-tester
36
+ ```
37
+ You will be asked to provide the following:
38
+ - URL
39
+ - HTTP Method (GET, POST, PUT, DELETE)
40
+ - Body (as JSON)
41
+ - Bearer Token (if needed)
42
+ - Custom Headers (optional)
43
+
44
+ 2. Command-Line Mode
45
+ You can also provide arguments directly to make the process faster.
46
+ ```bash
47
+ bin/api-tester -u https://your-api.com/login -m POST -H '{"Content-Type": "application/json"}' -b '{"email": "user@example.com", "password": "password123"}' -t 'your_bearer_token'
48
+ ```
49
+ Options:
50
+ - -u, --url : The API URL.
51
+ - -m, --method : The HTTP method (GET, POST, PUT, DELETE).
52
+ - -b, --body : The request body as JSON.
53
+ - -H, --headers : Custom headers as JSON.
54
+ - -t, --token : Bearer token for authorization.
55
+
56
+ 3. Response
57
+ The response from the server is displayed with colored status codes, headers in table format, and pretty-printed JSON body.
58
+
59
+ ## Logging
60
+
61
+ The tool provides colored logging to enhance visibility. Output includes details about the request, response status, and any errors encountered.
62
+
63
+ ## Best Practices
64
+
65
+ Respect the API's terms of service and rate limits.
66
+ Ensure you have permission to test authenticated endpoints.
67
+ Use caution with sensitive information when testing.
68
+
69
+ ## Contributing
70
+
71
+ Contributions are welcome! Report bugs and submit pull requests on GitHub at https://github.com/bhavyansh001/api_tester_cli. Please adhere to the Contributor Covenant code of conduct.
72
+
73
+ ## License
74
+ api_tester_cli is released under the MIT License. See the LICENSE file for details.
data/bin/api-tester ADDED
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'optparse'
5
+ require 'colorize'
6
+ require_relative '../lib/api_tester'
7
+
8
+ def send_request(options)
9
+ # Create and send request using options
10
+ request = ApiTester::Request.new
11
+ request.set_method(options[:method])
12
+ request.set_headers(options[:headers])
13
+ request.set_body(options[:body]) if options[:body]
14
+ request.set_auth_token(options[:token]) if options[:token]
15
+
16
+ client = ApiTester::Client.new(options[:url], request.headers, request.method, request.body, request.token)
17
+
18
+ # Display formatted request info
19
+ puts "\nSending request to: #{options[:url]}".green
20
+ puts "Method: #{options[:method]}".green
21
+ puts "Headers: #{options[:headers]}".green
22
+ puts "Body: #{options[:body].to_json}" if options[:body]
23
+
24
+ # Send the request and display the response
25
+ response = client.send_request
26
+ response.display
27
+ end
28
+
29
+ options = {}
30
+ OptionParser.new do |opts|
31
+ opts.banner = "Usage: api-tester [options]"
32
+
33
+ opts.on("-u", "--url URL", "API URL") { |v| options[:url] = v }
34
+ opts.on("-m", "--method METHOD", "HTTP method") { |v| options[:method] = v.upcase }
35
+ opts.on("-b", "--body BODY", "Request body as JSON") { |v| options[:body] = JSON.parse(v) }
36
+ opts.on("-H", "--headers HEADERS", "Request headers as JSON") { |v| options[:headers] = JSON.parse(v) }
37
+ opts.on("-t", "--token TOKEN", "Bearer token for authorization") { |v| options[:token] = v }
38
+ end.parse!
39
+
40
+ # If URL is provided via arguments (first case)
41
+ if options[:url]
42
+ send_request(options)
43
+
44
+ # If no URL provided, enter interactive mode (second case)
45
+ else
46
+ puts "Welcome to the API Tester!".light_blue
47
+ puts "Please provide the following details:"
48
+
49
+ print "Base URL (e.g., https://example.com/api/v1): ".yellow
50
+ base_url = gets.chomp
51
+
52
+ headers = { "Content-Type" => "application/json" }
53
+ print "Do you have any additional headers as JSON? (y/n): ".yellow
54
+ if gets.chomp.downcase == 'y'
55
+ print "Enter headers as JSON: ".yellow
56
+ additional_headers = JSON.parse(gets.chomp)
57
+ headers.merge!(additional_headers)
58
+ end
59
+ options[:headers] = headers
60
+
61
+ begin
62
+ loop do
63
+ print "Endpoint (e.g., /login): ".yellow
64
+ endpoint = gets.chomp
65
+ options[:url] = "#{base_url}#{endpoint}"
66
+
67
+ print "HTTP Method (GET, POST, PUT, DELETE): ".yellow
68
+ options[:method] = gets.chomp.upcase
69
+
70
+ print "Do you have a request body? (y/n): ".yellow
71
+ if gets.chomp.downcase == 'y'
72
+ print "Enter the body as JSON: ".yellow
73
+ options[:body] = JSON.parse(gets.chomp)
74
+ else
75
+ options[:body] = nil
76
+ end
77
+
78
+ print "Do you have a Bearer Token? (y/n): ".yellow
79
+ if gets.chomp.downcase == 'y'
80
+ print "Enter the Bearer Token: ".yellow
81
+ options[:token] = gets.chomp
82
+ else
83
+ options[:token] = nil
84
+ end
85
+
86
+ # Send request in loop
87
+ send_request(options)
88
+
89
+ puts "\nPress Ctrl+C to exit or continue with a new request.".light_blue
90
+ end
91
+ rescue Interrupt
92
+ puts "\nExiting API Tester. Goodbye!".red
93
+ end
94
+ end
@@ -0,0 +1,49 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module ApiTester
6
+ class Client
7
+ def initialize(url, headers = {}, method = 'GET', body = nil, token = nil)
8
+ @uri = URI.parse(url)
9
+ @headers = headers
10
+ @method = method
11
+ @body = body
12
+ @token = token
13
+ end
14
+
15
+ def send_request
16
+ http = Net::HTTP.new(@uri.host, @uri.port)
17
+ http.use_ssl = @uri.scheme == 'https' # Enable SSL for HTTPS
18
+
19
+ request = build_request
20
+ add_auth_token(request) if @token
21
+
22
+ response = http.request(request)
23
+ ApiTester::Response.new(response)
24
+ end
25
+
26
+ private
27
+
28
+ def build_request
29
+ case @method.upcase
30
+ when 'POST'
31
+ req = Net::HTTP::Post.new(@uri, @headers)
32
+ req.body = @body.to_json if @body # Convert body to JSON string
33
+ req
34
+ when 'PUT'
35
+ req = Net::HTTP::Put.new(@uri, @headers)
36
+ req.body = @body.to_json if @body
37
+ req
38
+ when 'DELETE'
39
+ Net::HTTP::Delete.new(@uri, @headers)
40
+ else
41
+ Net::HTTP::Get.new(@uri, @headers)
42
+ end
43
+ end
44
+
45
+ def add_auth_token(request)
46
+ request['Authorization'] = "Bearer #{@token}"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ module ApiTester
2
+ class Helper
3
+ def self.get_token_from_env
4
+ ENV['API_AUTH_TOKEN']
5
+ end
6
+
7
+ def self.format_json(json_string)
8
+ JSON.pretty_generate(JSON.parse(json_string))
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module ApiTester
2
+ class Request
3
+ attr_reader :headers, :method, :body, :token
4
+
5
+ def initialize
6
+ @headers = {}
7
+ @method = 'GET'
8
+ @body = nil
9
+ @token = nil
10
+ end
11
+
12
+ def set_headers(headers)
13
+ @headers = headers
14
+ end
15
+
16
+ def set_method(method)
17
+ @method = method.upcase
18
+ end
19
+
20
+ def set_body(body)
21
+ @body = body
22
+ end
23
+
24
+ def set_auth_token(token)
25
+ @token = token
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,62 @@
1
+ require 'terminal-table'
2
+
3
+ module ApiTester
4
+ class Response
5
+ def initialize(response)
6
+ @response = response
7
+ end
8
+
9
+ def display
10
+ # Display status with color
11
+ display_status
12
+
13
+ # Display headers in a table format
14
+ display_headers
15
+
16
+ # Display body
17
+ display_body
18
+ end
19
+
20
+ private
21
+
22
+ def display_status
23
+ # Use green for 2xx, yellow for 3xx, red for 4xx and 5xx
24
+ status_code = @response.code.to_i
25
+ color = case status_code
26
+ when 200..299 then :green
27
+ when 300..399 then :yellow
28
+ else :red
29
+ end
30
+
31
+ puts "\nStatus: #{@response.code}".colorize(color)
32
+ end
33
+
34
+ def display_headers
35
+ puts "\nHeaders:".light_blue
36
+
37
+ # Using Terminal::Table to format headers
38
+ headers_table = Terminal::Table.new do |t|
39
+ t.headings = ['Header', 'Value']
40
+ @response.each_header { |key, value| t.add_row([key, value]) }
41
+ end
42
+
43
+ puts headers_table
44
+ end
45
+
46
+ def display_body
47
+ puts "\nBody:".light_blue
48
+
49
+ if json_response?
50
+ # Pretty-print JSON with color
51
+ puts JSON.pretty_generate(JSON.parse(@response.body)).colorize(:light_green)
52
+ else
53
+ # Print plain body for non-JSON responses
54
+ puts @response.body.colorize(:light_yellow)
55
+ end
56
+ end
57
+
58
+ def json_response?
59
+ @response['Content-Type'] && @response['Content-Type'].include?('application/json')
60
+ end
61
+ end
62
+ end
data/lib/api_tester.rb ADDED
@@ -0,0 +1,8 @@
1
+ module ApiTester
2
+ end
3
+
4
+ # Require all components
5
+ require_relative 'api_tester/client'
6
+ require_relative 'api_tester/request'
7
+ require_relative 'api_tester/response'
8
+ require_relative 'api_tester/helper'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-tester-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bhavyansh Yadav
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: terminal-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.13.0
69
+ description: This tool allows users to interactively test APIs via the command line.
70
+ email: bhavyansh001@gmail.com
71
+ executables:
72
+ - api-tester
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - bin/api-tester
78
+ - lib/api_tester.rb
79
+ - lib/api_tester/client.rb
80
+ - lib/api_tester/helper.rb
81
+ - lib/api_tester/request.rb
82
+ - lib/api_tester/response.rb
83
+ homepage: https://rubygems.org/gems/api_tester
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ source_code_uri: https://github.com/bhavyansh001/api_tester
88
+ issue_tracker_uri: https://github.com/bhavyansh001/api_tester/issues
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 3.3.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.5.18
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: A CLI tool to test APIs
108
+ test_files: []