From 41e6fd2cdc15b18d05825915b191046f360fe4ea Mon Sep 17 00:00:00 2001 From: Hannah-Dagemark Date: Mon, 20 Nov 2023 10:16:45 +0100 Subject: [PATCH] Initial Commit Base Script with a Rake Verification --- Gemfile | 3 + Gemfile.lock | 13 +++++ lib/Request.rb | 56 +++++++++++++++++++ rakefile | 10 ++++ .../example_requests/get-examples.request.txt | 6 ++ .../get-fruits-with-filter.request.txt | 6 ++ spec/example_requests/get-index.request.txt | 4 ++ spec/example_requests/post-login.request.txt | 6 ++ spec/http_parser_spec.rb | 25 +++++++++ spec/spec_helper.rb | 1 + testing.rb | 41 ++++++++++++++ 11 files changed, 171 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 lib/Request.rb create mode 100644 rakefile create mode 100644 spec/example_requests/get-examples.request.txt create mode 100644 spec/example_requests/get-fruits-with-filter.request.txt create mode 100644 spec/example_requests/get-index.request.txt create mode 100644 spec/example_requests/post-login.request.txt create mode 100644 spec/http_parser_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 testing.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..939c93f --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'minitest' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..8169cd9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + minitest (5.20.0) + +PLATFORMS + x64-mingw-ucrt + +DEPENDENCIES + minitest + +BUNDLED WITH + 2.4.19 diff --git a/lib/Request.rb b/lib/Request.rb new file mode 100644 index 0000000..daf0f15 --- /dev/null +++ b/lib/Request.rb @@ -0,0 +1,56 @@ +class Request + attr_reader :method, :resource, :version, :headers, :params + + def initialize(input) + @input = input + @method = "" + @resource = "" + @version = "" + @headers = [] + @params = [] + deconstructor(@input) + end + + def deconstructor(input) + rows = input.split(/\n/) + @method, @resource, @version = rows[0].split(' ') + @headers, @params = {}, {} + if rows.find_index("") != nil then + limit = rows.find_index("") + else + limit = rows.length + end + header_constructor(rows, limit) + if limit != rows.length + param_constructor(rows) + end + if @resource.include?("?") + resource_deconstructor + end + end + def header_constructor(rows, limit) + for row in rows[1..limit-1] + context, information = row.split(' ') + context = context.sub(/:/, "") + @headers[context] = information + end + end + + def param_constructor(rows) + for row in rows[rows.find_index("")+1..rows.length] + information = row.split(/&/) + for info in information + context, content = info.split('=') + @params[context] = content + end + end + end + + def resource_deconstructor + information = @resource.split('?')[1].split(/&/) + for info in information + context, content = info.split('=') + @params[context] = content + end + end +end \ No newline at end of file diff --git a/rakefile b/rakefile new file mode 100644 index 0000000..7d15524 --- /dev/null +++ b/rakefile @@ -0,0 +1,10 @@ +require "minitest/test_task" + +Minitest::TestTask.create(:test) do |t| + t.libs << "spec" + t.libs << "lib" + t.warning = false + t.test_globs = ["spec/**/*_spec.rb"] +end + +task :default => :test \ No newline at end of file diff --git a/spec/example_requests/get-examples.request.txt b/spec/example_requests/get-examples.request.txt new file mode 100644 index 0000000..eedd945 --- /dev/null +++ b/spec/example_requests/get-examples.request.txt @@ -0,0 +1,6 @@ +GET /examples HTTP/1.1 +Host: example.com +User-Agent: ExampleBrowser/1.0 +Accept-Encoding: gzip, deflate +Accept: */* + diff --git a/spec/example_requests/get-fruits-with-filter.request.txt b/spec/example_requests/get-fruits-with-filter.request.txt new file mode 100644 index 0000000..84098ac --- /dev/null +++ b/spec/example_requests/get-fruits-with-filter.request.txt @@ -0,0 +1,6 @@ +GET /fruits?type=bananas&minrating=4 HTTP/1.1 +Host: fruits.com +User-Agent: ExampleBrowser/1.0 +Accept-Encoding: gzip, deflate +Accept: */* + diff --git a/spec/example_requests/get-index.request.txt b/spec/example_requests/get-index.request.txt new file mode 100644 index 0000000..dcb2d76 --- /dev/null +++ b/spec/example_requests/get-index.request.txt @@ -0,0 +1,4 @@ +GET / HTTP/1.1 +Host: developer.mozilla.org +Accept-Language: fr + diff --git a/spec/example_requests/post-login.request.txt b/spec/example_requests/post-login.request.txt new file mode 100644 index 0000000..5e9a407 --- /dev/null +++ b/spec/example_requests/post-login.request.txt @@ -0,0 +1,6 @@ +POST /login HTTP/1.1 +Host: foo.example +Content-Type: application/x-www-form-urlencoded +Content-Length: 39 + +username=grillkorv&password=verys3cret! \ No newline at end of file diff --git a/spec/http_parser_spec.rb b/spec/http_parser_spec.rb new file mode 100644 index 0000000..4b4ef4c --- /dev/null +++ b/spec/http_parser_spec.rb @@ -0,0 +1,25 @@ +require_relative 'spec_helper' +require_relative '../lib/Request' + +describe 'Request' do + + describe 'Simple get-request' do + + it 'parses the http verb' do + @request = Request.new(File.read('./spec/example_requests/get-index.request.txt')) + _(@request.method).must_equal "GET" + end + + it 'parses the resource' do + @request = Request.new(File.read('./spec/example_requests/get-index.request.txt')) + _(@request.resource).must_equal "/" + end + + + end + + describe 'get-requests with url params' do + + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8c0ac88 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require 'minitest/autorun' \ No newline at end of file diff --git a/testing.rb b/testing.rb new file mode 100644 index 0000000..8306cc9 --- /dev/null +++ b/testing.rb @@ -0,0 +1,41 @@ +require_relative 'main' + +request_string = File.read('testcases/get-index.request.txt') + +request = Request.new(request_string) + +p request.method +p request.resource +p request.version +p request.headers +p request.params + +request_string = File.read('testcases/get-examples.request.txt') + +request = Request.new(request_string) + +p request.method +p request.resource +p request.version +p request.headers +p request.params + +request_string = File.read('testcases/get-fruits-with-filter.request.txt') + +request = Request.new(request_string) + +p request.method +p request.resource +p request.version +p request.headers +p request.params + +request_string = File.read('testcases/post-login.request.txt') + +request = Request.new(request_string) + +p request.method +p request.resource +p request.version +p request.headers +p request.params \ No newline at end of file