Skip to content

Commit

Permalink
Merge pull request #129 from hahwul/dev
Browse files Browse the repository at this point in the history
Release v0.9.1
  • Loading branch information
hahwul authored Oct 6, 2023
2 parents c61d50a + 67af9c7 commit 35147d4
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 173 deletions.
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.9.0
version: 0.9.1

authors:
- hahwul <[email protected]>
Expand Down
10 changes: 10 additions & 0 deletions spec/functional_test/fixtures/js_express/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require('express')

module.exports = function(app) {
app.get('/',function(req,res){
res.render('index');
});
app.post('/upload',function(req,res){
res.render('index');
});
}
7 changes: 7 additions & 0 deletions spec/functional_test/fixtures/oas3/nil_cast/doc_nil_cast.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
openapi: 3.0.0
info:
title: Gem Store API
version: 1.0.0
description: Sample API for managing pets in a Gem store.
paths:
/gems:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
openapi: 3.0.0
info:
title: Gem Store API
version: 1.0.0
description: Sample API for managing pets in a Gem store.
paths:
11 changes: 11 additions & 0 deletions spec/functional_test/testers/js_express_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "../func_spec.cr"

extected_endpoints = [
Endpoint.new("/", "GET"),
Endpoint.new("/upload", "POST"),
]

FunctionalTester.new("fixtures/js_express/", {
:techs => 1,
:endpoints => 2,
}, extected_endpoints).test_all
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 @@ -21,3 +21,8 @@ FunctionalTester.new("fixtures/oas3/multiple_docs/", {
:techs => 1,
:endpoints => 2,
}, nil).test_all

FunctionalTester.new("fixtures/oas3/nil_cast/", {
:techs => 1,
:endpoints => 0,
}, nil).test_all
6 changes: 6 additions & 0 deletions spec/unit_test/models/deliver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ describe "Initialize" do
object = Deliver.new options
object.proxy.should eq("http://localhost:8090")
end

it "Deliver with headers" do
options[:send_with_headers] = "X-API-Key: abcdssss"
object = Deliver.new options
object.headers["X-API-Key"].should eq("abcdssss")
end
end
102 changes: 51 additions & 51 deletions src/analyzer/analyzers/analyzer_express.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,70 @@ require "../../models/analyzer"

class AnalyzerExpress < Analyzer
def analyze
# TODO
end
end

def analyzer_express(options : Hash(Symbol, String))
result = [] of Endpoint
base_path = options[:base]
url = options[:url]
_ = url

# Source Analysis
begin
Dir.glob("#{base_path}/**/*") do |path|
next if File.directory?(path)
if File.exists?(path)
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
file.each_line do |line|
if line.includes? ".get('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(api_path, "GET")
# Source Analysis
begin
Dir.glob("#{base_path}/**/*") do |path|
next if File.directory?(path)
if File.exists?(path)
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
file.each_line do |line|
if line.includes? ".get('/"
api_path = express_get_endpoint(line)
if api_path != ""
endpoint = (url + api_path).gsub(/\/\//, "/")
result << Endpoint.new(endpoint, "GET")
end
end
end
if line.includes? ".post('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(api_path, "POST")
if line.includes? ".post('/"
api_path = express_get_endpoint(line)
if api_path != ""
endpoint = (url + api_path).gsub(/\/\//, "/")
result << Endpoint.new(endpoint, "POST")
end
end
end
if line.includes? ".put('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(api_path, "PUT")
if line.includes? ".put('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(url + api_path, "PUT")
end
end
end
if line.includes? ".delete('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(api_path, "DELETE")
if line.includes? ".delete('/"
api_path = express_get_endpoint(line)
if api_path != ""
endpoint = (url + api_path).gsub(/\/\//, "/")
result << Endpoint.new(endpoint, "DELETE")
end
end
end
if line.includes? ".patch('/"
api_path = express_get_endpoint(line)
if api_path != ""
result << Endpoint.new(api_path, "PATCH")
if line.includes? ".patch('/"
api_path = express_get_endpoint(line)
if api_path != ""
endpoint = (url + api_path).gsub(/\/\//, "/")
result << Endpoint.new(endpoint, "PATCH")
end
end
end
end
end
end
rescue e
# TODO
end
rescue e
# TODO

result
end

result
end
def express_get_endpoint(line : String)
api_path = ""
splited = line.split("(")
if splited.size > 0
api_path = splited[1].split(",")[0].gsub(/['"]/, "")
end

def express_get_endpoint(line : String)
api_path = ""
splited = line.split("(")
if splited.size > 0
api_path = splited[1].split(",")[0].gsub(/['"]/, "")
api_path
end
end

api_path
def analyzer_express(options : Hash(Symbol, String))
instance = AnalyzerExpress.new(options)
instance.analyze
end
118 changes: 74 additions & 44 deletions src/analyzer/analyzers/analyzer_oas2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,49 @@ class AnalyzerOAS2 < Analyzer
if json_obj["basePath"].to_s != ""
base_path = base_path + json_obj["basePath"].to_s
end
rescue
rescue e
@logger.debug "Exception of #{swagger_json}/basePath"
@logger.debug_sub e
end
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
begin
paths = json_obj["paths"].as_h
paths.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
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)
rescue e
@logger.debug "Exception of #{swagger_json}/paths/path/method"
@logger.debug_sub e
end
rescue e
@logger.debug "Exception of #{swagger_json}/paths/path"
@logger.debug_sub e
end
rescue e
@logger.debug "Exception of #{swagger_json}/paths"
@logger.debug_sub e
end
end
end
Expand All @@ -59,34 +74,49 @@ class AnalyzerOAS2 < Analyzer
if yaml_obj["basePath"].to_s != ""
base_path = base_path + yaml_obj["basePath"].to_s
end
rescue
rescue e
@logger.debug "Exception of #{swagger_yaml}/basePath"
@logger.debug_sub e
end
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
begin
paths = yaml_obj["paths"].as_h
paths.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
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)
rescue e
@logger.debug "Exception of #{swagger_yaml}/paths/path/method"
@logger.debug_sub e
end
rescue e
@logger.debug "Exception of #{swagger_yaml}/paths/path"
@logger.debug_sub e
end
rescue e
@logger.debug "Exception of #{swagger_yaml}/paths"
@logger.debug_sub e
end
end
end
Expand Down
Loading

0 comments on commit 35147d4

Please sign in to comment.