file_sv 0.1.11 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f83c739611ce58614e8d076e6be444b889ff63727b174dcfbd13025e3367b2d6
4
- data.tar.gz: 042d4b3ae8ad91e6708b19bcdf9ad5b3473682494a8a6c4879ef50e61dbcce29
3
+ metadata.gz: c69a4a4dec09d95aa3ec0ef6aa3871c2f3d53b22a0ec265b8b270a2a940a8177
4
+ data.tar.gz: 0bad44a25fe5e26efc4145290d939b2298912c9b185def00b92a8fc57db2acb9
5
5
  SHA512:
6
- metadata.gz: fbe538e3dad13e5bdd4af99c8d10b792397e5a1252c2a9809a5120a9aec9302a5d83aff05f3a36d9ef112a3c335184c2c97d4668fa9e44f25fe2ffe319b9175c
7
- data.tar.gz: d5427782a507a555b305578c50354405f89b6307824d810916a529018f392c541f003f3068d96d257fb8d8ea9460bae38b6db176cba33210bbca62edb1636cbb
6
+ metadata.gz: 2fefabfaf7af9116e9ecdf160151dd081112aa591f28e5b231719732c079d4f6fbc4973afc964b4a8295611d19a3d798c085a5d50d173fbe188477fbf6dee8dc
7
+ data.tar.gz: 177338aca5865d57008934cea16600890048effa3ce33ace8fd36437df0855250d89d977702c683c57d4dfb6a5904fed3769b6a6eaf131a1b3340ce333a4435d
data/exe/file_sv CHANGED
@@ -6,16 +6,23 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
6
 
7
7
  # Executable for Generic Test
8
8
  class Exe < Thor
9
+ def self.exit_on_failure?
10
+ true
11
+ end
12
+
9
13
  desc "plan folder", "Show plan for service virtualization based on folder"
10
- def plan(folder)
14
+ def plan(folder = nil)
11
15
  require "file_sv"
16
+ if (!folder && GlobalSettings.serving_folder)
17
+ folder = GlobalSettings.serving_folder unless folder
18
+ end
12
19
  ServiceLoader.create_plan_for folder
13
20
  end
14
21
 
15
22
  option :crt, default: nil, banner: "HTTPS CRT"
16
23
  option :key, default: nil, banner: "HTTPS key"
17
24
  desc "serve folder", "Serve virtual service based on folder"
18
- def serve(folder)
25
+ def serve(folder = nil)
19
26
  plan folder
20
27
  ServiceLoader.serve_plan options
21
28
  end
@@ -26,6 +33,13 @@ class Exe < Thor
26
33
  ServiceLoader.inspect folder
27
34
  end
28
35
 
36
+ desc "generate har_file_location mock_service_base", "Generate mock folder/file structure from recording"
37
+ def generate(har_file_location, mock_service_base, output_folder)
38
+ require "file_sv/har_generator"
39
+ generator = HarGenerator.new(har_file_location, mock_service_base, output_folder)
40
+ generator.generate
41
+ end
42
+
29
43
  desc "version", "Version of FileSv"
30
44
  def version
31
45
  require "file_sv/version"
@@ -10,6 +10,8 @@ class GlobalSettings
10
10
 
11
11
  @https = false
12
12
 
13
+ @serving_folder = '.'
14
+
13
15
  @ignore_status_codes = nil
14
16
  class << self
15
17
  # @return [String] Default REST method when none specified by filename
@@ -26,6 +28,8 @@ class GlobalSettings
26
28
  attr_accessor :key
27
29
  # @return [Array] List of http status codes to ignore
28
30
  attr_accessor :ignore_status_codes
31
+ # @return [String] Path from current location to serve folders from
32
+ attr_accessor :serving_folder
29
33
 
30
34
  # @param [Integer] status_code HTTP status code
31
35
  # @return [Boolean] Whether status code is currently ignored
@@ -0,0 +1,78 @@
1
+ require_relative '../file_sv'
2
+ require 'json'
3
+ require 'fileutils'
4
+
5
+ class HarGenerator
6
+
7
+ attr_accessor :har_file_location
8
+
9
+ attr_accessor :mock_service_base
10
+
11
+ attr_accessor :output_folder
12
+ # @return [String] Har file content
13
+ attr_accessor :content
14
+
15
+ attr_accessor :entries
16
+
17
+ def initialize(har_file_location, mock_service_base, output_folder)
18
+ self.har_file_location = har_file_location
19
+ self.mock_service_base = mock_service_base
20
+ self.output_folder = output_folder
21
+ self.content = File.read(har_file_location)
22
+ parse_har
23
+ end
24
+
25
+ def parse_har
26
+ parsed = JSON.parse(self.content)
27
+ pages = parsed['log']['pages']
28
+ self.entries = parsed['log']['entries']
29
+ puts "Found #{self.entries.count} entries in #{self.har_file_location}"
30
+ end
31
+
32
+ def generate
33
+ self.entries.each do |entry|
34
+ request = entry['request']
35
+ response = entry['response']
36
+
37
+ request_url = request['url']
38
+ if (!request_url.include?(self.mock_service_base))
39
+ next;
40
+ end
41
+
42
+ path = request_url[self.mock_service_base.length..-1]
43
+ path_folder = path.split('.')[0]
44
+
45
+ method = request['method']
46
+ status_code = response['status']
47
+ content = response['content']
48
+ mime_type = content['mimeType']
49
+ extension = mime_type.split('/').last
50
+
51
+ extension = 'js' if (extension == 'javascript')
52
+
53
+ location = "#{self.output_folder}/#{path_folder}/#{method}_#{status_code}.#{extension}"
54
+ create_file_at "./#{location}", extension, content['text']
55
+ end
56
+ end
57
+
58
+ # Create folder if there's not a file already there.
59
+ # Will create parent folder if necessary.
60
+ # @param [String] folder Folder to create
61
+ def create_folder(folder)
62
+ if File.exist? folder
63
+ warn "!! #{folder} already exists and is not a directory" unless File.directory? folder
64
+ else
65
+ FileUtils.mkdir_p folder
66
+ puts "Created folder: #{folder}"
67
+ end
68
+ end
69
+
70
+ def create_file_at(file_path, extension, content)
71
+ file_path = file_path.gsub('//', '/')
72
+ folder_name = File.split(file_path).first
73
+ folder_name += ".#{extension}" if (extension == '.css' || extension == '.js')
74
+ create_folder folder_name
75
+ puts "Creating response at #{file_path}"
76
+ File.open("#{folder_name}/#{File.split(file_path).last}", 'w') { |f| f.puts content }
77
+ end
78
+ end
@@ -52,6 +52,9 @@ class PlannedEndpoint
52
52
  self.status_code = GlobalSettings.empty_body_status
53
53
  return true
54
54
  end
55
+
56
+ return false
57
+ rescue Encoding::CompatibilityError
55
58
  false
56
59
  end
57
60
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileSv
4
- VERSION = "0.1.11"
4
+ VERSION = "0.1.13"
5
5
  end
data/lib/file_sv.rb CHANGED
@@ -30,12 +30,18 @@ CONFIG_FILE = "file_sv.yaml"
30
30
 
31
31
  # Set values in global settings based on config
32
32
  def load_default_config(file_path)
33
- return unless File.exist? file_path
33
+ unless File.exist? file_path
34
+ puts "No config found at #{file_path}" if ENV['debug'] == "true"
35
+ return
36
+ end
37
+ puts "Loading config from #{file_path}" if ENV['debug'] == "true"
34
38
 
35
39
  config = YAML.load_file file_path
36
40
  return unless config # Handle empty YAML file
37
41
 
42
+ puts "Config #{config}" if ENV['debug'] == "true"
38
43
  config["global"]&.each do |key, value|
44
+ puts "Setting #{key} to #{value}" if ENV['debug'] == "true"
39
45
  GlobalSettings.send("#{key}=", value)
40
46
  end
41
47
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_sv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-20 00:00:00.000000000 Z
11
+ date: 2024-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -109,6 +109,7 @@ files:
109
109
  - lib/file_sv/file_processor.rb
110
110
  - lib/file_sv/file_sv.ico
111
111
  - lib/file_sv/global_settings.rb
112
+ - lib/file_sv/har_generator.rb
112
113
  - lib/file_sv/planned_endpoint.rb
113
114
  - lib/file_sv/render_file.rb
114
115
  - lib/file_sv/service_loader.rb
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  - !ruby/object:Gem::Version
139
140
  version: '0'
140
141
  requirements: []
141
- rubygems_version: 3.2.3
142
+ rubygems_version: 3.5.3
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: REST service virtualisation through file structure.