Initial Commit

Base Script with a Rake Verification
This commit is contained in:
Hannah-Dagemark 2023-11-20 10:16:45 +01:00
commit 41e6fd2cdc
11 changed files with 171 additions and 0 deletions

3
Gemfile Normal file
View file

@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'minitest'

13
Gemfile.lock Normal file
View file

@ -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

56
lib/Request.rb Normal file
View file

@ -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

10
rakefile Normal file
View file

@ -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

View file

@ -0,0 +1,6 @@
GET /examples HTTP/1.1
Host: example.com
User-Agent: ExampleBrowser/1.0
Accept-Encoding: gzip, deflate
Accept: */*

View file

@ -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: */*

View file

@ -0,0 +1,4 @@
GET / HTTP/1.1
Host: developer.mozilla.org
Accept-Language: fr

View file

@ -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!

25
spec/http_parser_spec.rb Normal file
View file

@ -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

1
spec/spec_helper.rb Normal file
View file

@ -0,0 +1 @@
require 'minitest/autorun'

41
testing.rb Normal file
View file

@ -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