Skip to content

Commit

Permalink
Merge pull request #113 from hahwul/dev
Browse files Browse the repository at this point in the history
Release 0.8.0
  • Loading branch information
hahwul authored Sep 26, 2023
2 parents 6276ecc + e753d27 commit 97eaa33
Show file tree
Hide file tree
Showing 31 changed files with 813 additions and 405 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Usage: noir <flags>
-s SCOPE, --scope url,param Set scope for detection
Output:
-f FORMAT, --format json Set output format [plain/json/markdown-table/curl/httpie]
-f FORMAT, --format json Set output format [plain/json/markdown-table/curl/httpie/oas2/oas3]
-o PATH, --output out.txt Write result to file
--set-pvalue VALUE Specifies the value of the identified parameter
--no-color Disable color output
Expand All @@ -97,6 +97,7 @@ Usage: noir <flags>
Deliver:
--send-req Send the results to the web request
--send-proxy http://proxy.. Send the results to the web request via http proxy
--with-headers X-Header:Value Add Custom Headers to be Used in Deliver
Technologies:
-t TECHS, --techs rails,php Set technologies to use
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: noir
version: 0.7.3
version: 0.8.0

authors:
- hahwul <[email protected]>
Expand Down
21 changes: 21 additions & 0 deletions spec/functional_test/fixtures/oas3/multiple_docs/first.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.0
info:
title: Gem Store API
version: 1.0.0
description: Sample API for managing pets in a Gem store.
servers:
- url: 'https://api.example.com/v1'
paths:
/gems:
get:
summary: Get a list of pets
responses:
'200':
description: A list of pets
content:
application/json:
example:
- id: 1
name: Ruby
- id: 2
name: Crystal
21 changes: 21 additions & 0 deletions spec/functional_test/fixtures/oas3/multiple_docs/second.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.0
info:
title: Gem Store API
version: 1.0.0
description: Sample API for managing pets in a Gem store.
servers:
- url: 'https://api.example.com/v1'
paths:
/shards:
get:
summary: Get a list of pets
responses:
'200':
description: A list of pets
content:
application/json:
example:
- id: 1
name: Ruby
- id: 2
name: Crystal
2 changes: 2 additions & 0 deletions spec/functional_test/func_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class FunctionalTester

def test_all
describe "Functional test to #{@path}" do
locator = CodeLocator.instance
locator.clear_all
test_detect
test_analyze
end
Expand Down
5 changes: 5 additions & 0 deletions spec/functional_test/testers/oas3_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ FunctionalTester.new("fixtures/oas3/no_servers/", {
:techs => 1,
:endpoints => 1,
}, nil).test_all

FunctionalTester.new("fixtures/oas3/multiple_docs/", {
:techs => 1,
:endpoints => 2,
}, nil).test_all
5 changes: 3 additions & 2 deletions spec/unit_test/detector/detect_oas2_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ describe "Detect OAS 2.0(Swagger) Docs" do
}
EOS

instance.detect("docs.json", content)
locator = CodeLocator.instance
locator.get("swagger-json").should eq("docs.json")
locator.clear "swagger-json"
instance.detect("docs.json", content)
locator.all("swagger-json").should eq(["docs.json"])
end
end
5 changes: 3 additions & 2 deletions spec/unit_test/detector/detect_oas3_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ describe "Detect OAS 3.0 Docs" do
}
EOS

instance.detect("docs.json", content)
locator = CodeLocator.instance
locator.get("oas3-json").should eq("docs.json")
locator.clear "oas3-json"
instance.detect("docs.json", content)
locator.all("oas3-json").should eq(["docs.json"])
end
end
8 changes: 7 additions & 1 deletion spec/unit_test/models/code_locator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ require "../../../src/options.cr"
describe "Initialize" do
locator = CodeLocator.new

it "getter/setter - name" do
it "getter/setter - string" do
locator.set "unittest", "abcd"
locator.get("unittest").should eq("abcd")
end

it "all/push - array" do
locator.push "unittest", "abcd"
locator.push "unittest", "bbbb"
locator.all("unittest").should eq(["abcd", "bbbb"])
end
end
13 changes: 13 additions & 0 deletions spec/unit_test/models/deliver_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "../../../src/models/deliver.cr"
require "../../../src/options.cr"

describe "Initialize" do
options = default_options
options[:base] = "noir"
options[:send_proxy] = "http://localhost:8090"

it "Deliver" do
object = Deliver.new options
object.proxy.should eq("http://localhost:8090")
end
end
43 changes: 43 additions & 0 deletions spec/unit_test/models/output_builder_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "../../../src/models/output_builder.cr"
require "../../../src/options.cr"

describe "Initialize" do
options = default_options
options[:base] = "noir"
options[:scope] = "param"

it "OutputBuilder" do
object = OutputBuilder.new options
object.scope.should eq("param")
end

it "OutputBuilderCommon" do
object = OutputBuilderCommon.new options
object.scope.should eq("param")
end

it "OutputBuilderCurl" do
object = OutputBuilderCurl.new options
object.scope.should eq("param")
end

it "OutputBuilderHttpie" do
object = OutputBuilderHttpie.new options
object.scope.should eq("param")
end

it "OutputBuilderMarkdownTable" do
object = OutputBuilderMarkdownTable.new options
object.scope.should eq("param")
end

it "OutputBuilderOas2" do
object = OutputBuilderOas2.new options
object.scope.should eq("param")
end

it "OutputBuilderOas3" do
object = OutputBuilderOas3.new options
object.scope.should eq("param")
end
end
132 changes: 68 additions & 64 deletions src/analyzer/analyzers/analyzer_oas2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,89 @@ require "../../models/analyzer"
class AnalyzerOAS2 < Analyzer
def analyze
locator = CodeLocator.instance
swagger_json = locator.get("swagger-json")
swagger_yaml = locator.get("swagger-yaml")
swagger_jsons = locator.all("swagger-json")
swagger_yamls = locator.all("swagger-yaml")

if !swagger_json.nil?
if File.exists?(swagger_json)
content = File.read(swagger_json, encoding: "utf-8", invalid: :skip)
json_obj = JSON.parse(content)
base_path = @url
begin
if json_obj["basePath"].to_s != ""
base_path = base_path + json_obj["basePath"].to_s
if swagger_jsons.is_a?(Array(String))
swagger_jsons.each do |swagger_json|
if File.exists?(swagger_json)
content = File.read(swagger_json, encoding: "utf-8", invalid: :skip)
json_obj = JSON.parse(content)
base_path = @url
begin
if json_obj["basePath"].to_s != ""
base_path = base_path + json_obj["basePath"].to_s
end
rescue
end
rescue
end
json_obj["paths"].as_h.each do |path, path_obj|
path_obj.as_h.each do |method, method_obj|
params = [] of Param
json_obj["paths"].as_h.each do |path, path_obj|
path_obj.as_h.each do |method, method_obj|
params = [] of Param

if method_obj.as_h.has_key?("parameters")
method_obj["parameters"].as_a.each do |param_obj|
param_name = param_obj["name"].to_s
if param_obj["in"] == "query"
param = Param.new(param_name, "", "query")
params << param
elsif param_obj["in"] == "form"
param = Param.new(param_name, "", "json")
params << param
elsif param_obj["in"] == "formData"
param = Param.new(param_name, "", "form")
params << param
elsif param_obj["in"] == "header"
param = Param.new(param_name, "", "header")
params << param
if method_obj.as_h.has_key?("parameters")
method_obj["parameters"].as_a.each do |param_obj|
param_name = param_obj["name"].to_s
if param_obj["in"] == "query"
param = Param.new(param_name, "", "query")
params << param
elsif param_obj["in"] == "form"
param = Param.new(param_name, "", "json")
params << param
elsif param_obj["in"] == "formData"
param = Param.new(param_name, "", "form")
params << param
elsif param_obj["in"] == "header"
param = Param.new(param_name, "", "header")
params << param
end
end
@result << Endpoint.new(base_path + path, method.upcase, params)
else
@result << Endpoint.new(base_path + path, method.upcase)
end
@result << Endpoint.new(base_path + path, method.upcase, params)
else
@result << Endpoint.new(base_path + path, method.upcase)
end
end
end
end
end

if !swagger_yaml.nil?
if File.exists?(swagger_yaml)
content = File.read(swagger_yaml, encoding: "utf-8", invalid: :skip)
yaml_obj = YAML.parse(content)
base_path = @url
begin
if yaml_obj["basePath"].to_s != ""
base_path = base_path + yaml_obj["basePath"].to_s
if swagger_yamls.is_a?(Array(String))
swagger_yamls.each do |swagger_yaml|
if File.exists?(swagger_yaml)
content = File.read(swagger_yaml, encoding: "utf-8", invalid: :skip)
yaml_obj = YAML.parse(content)
base_path = @url
begin
if yaml_obj["basePath"].to_s != ""
base_path = base_path + yaml_obj["basePath"].to_s
end
rescue
end
rescue
end
yaml_obj["paths"].as_h.each do |path, path_obj|
path_obj.as_h.each do |method, method_obj|
params = [] of Param
yaml_obj["paths"].as_h.each do |path, path_obj|
path_obj.as_h.each do |method, method_obj|
params = [] of Param

if method_obj.as_h.has_key?("parameters")
method_obj["parameters"].as_a.each do |param_obj|
param_name = param_obj["name"].to_s
if param_obj["in"] == "query"
param = Param.new(param_name, "", "query")
params << param
elsif param_obj["in"] == "form"
param = Param.new(param_name, "", "json")
params << param
elsif param_obj["in"] == "formData"
param = Param.new(param_name, "", "form")
params << param
elsif param_obj["in"] == "header"
param = Param.new(param_name, "", "header")
params << param
if method_obj.as_h.has_key?("parameters")
method_obj["parameters"].as_a.each do |param_obj|
param_name = param_obj["name"].to_s
if param_obj["in"] == "query"
param = Param.new(param_name, "", "query")
params << param
elsif param_obj["in"] == "form"
param = Param.new(param_name, "", "json")
params << param
elsif param_obj["in"] == "formData"
param = Param.new(param_name, "", "form")
params << param
elsif param_obj["in"] == "header"
param = Param.new(param_name, "", "header")
params << param
end
end
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase, params)
else
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase)
end
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase, params)
else
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase)
end
end
end
Expand Down
Loading

0 comments on commit 97eaa33

Please sign in to comment.