Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions RackApp/RackApp/templates/template.html.erb

This file was deleted.

4 changes: 0 additions & 4 deletions RackApp/RackApp/templates/template.json.erb

This file was deleted.

5 changes: 0 additions & 5 deletions RackApp/RackApp/templates/template.xml.erb

This file was deleted.

7 changes: 7 additions & 0 deletions RackApp/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'myproject/app'

app = Rack::Builder.new do
run MyApp.new
end

run app
7 changes: 3 additions & 4 deletions RackApp/RackApp/app.rb → RackApp/lib/myproject/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def initialize(name, format)
end

def file(name, format)
file_path = File.expand_path(File.join(__dir__, 'templates', "#{name}.#{format}.erb"), __FILE__)
file_path = File.expand_path(File.join('../../..', 'templates', "#{name}.#{format}.erb"), __FILE__)
if File.exist?(file_path)
File.read(File.join(__dir__, 'templates', "#{name}.#{format}.erb"))
File.read(file_path)
else
'ERROR:The file doesnot exist'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise an error instead ;)
Otherwise the result of the file will be displayed to the User
It is better to catch the error when it is really necessary and not assuming what it should do

end
Expand Down Expand Up @@ -65,6 +65,7 @@ def initialize(app = nil)
def call(env)
@app&.call(env)
req = Rack::Request.new(env)
puts env.inspect
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this when you are done

timezone = req.params['timezone']
format = req.params['format']
nickname_match = req.path.match('/users/nickname/(\w+)')
Expand Down Expand Up @@ -94,5 +95,3 @@ def response_format(format)
end
end
end

Rack::Server.start(app: MyApp.new).new
7 changes: 7 additions & 0 deletions RackApp/templates/template.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
<b>
persons nick name is <%= person.nickname %> and current time in the timezone is <%= time.timezone %>
</b>
</head>
</html>
4 changes: 4 additions & 0 deletions RackApp/templates/template.json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name" : "<%= person.nickname%>", "time" : "<%= time.timezone %>"

}
5 changes: 5 additions & 0 deletions RackApp/templates/template.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<output>
<person><%= person.nickname %></person>
<time><%= time.timezone %></time>
</output>
62 changes: 62 additions & 0 deletions RackApp/test/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require 'test_helper'
require 'active_support/json'
require 'nokogiri'
require 'active_support/core_ext'

OUTER_APP = Rack::Builder.parse_file('config.ru').first

class TestApp < TestBase
def app
OUTER_APP
end

def test_jsonoutput
puts Time.zone = 'UTC'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are assigning Time.zone to 'UTC', is it intended?

puts @timezone = Time.zone.now
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the debugging lines when all is clean

data = { 'name' => 'hema', 'time' => @timezone.to_s }
json_data = JSON.parse(data.to_json).to_s
get '/users/nickname/hema?timezone=UTC&format=json'
puts response = JSON.parse(last_response.body).to_s
assert_equal response, json_data
end

def test_xmloutput
puts Time.zone = 'UTC'
puts @timezone = Time.zone.now
data = { 'person' => 'hema', 'time' => @timezone.to_s }
puts data1 = data.to_xml(root: 'output')
get '/users/nickname/hema?timezone=UTC&format=xml'
assert_equal last_response.body, data1
end

def test_htmloutput
personname = 'hema'
Time.zone = 'UTC'
puts @timezone = Time.zone.now
data = <<-HTML
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing the whole website is not the best idea. You would need to change this test every time you change a view HTML. Better to use https://apidock.com/ruby/Test/Unit/Assertions/assert_match and check for just small strings, something like:

assert_match "name is #{personname} and current", last_response.body

got the idea?

<html>
<head>
<b>
persons nick name is #{personname} and current time in the timezone is #{@timezone}
</b>
</head>
</html>
HTML
puts parsed_data = Nokogiri::HTML.parse(data).to_s
get '/users/nickname/hema?timezone=UTC&format=html'
response = Nokogiri::HTML.parse(last_response.body).to_s
assert_equal last_response.body, parsed_data
end

def test_pagenotfound
get '/users/nickname'
assert_equal last_response.body, '<html><body><b><em>404 Page not found</em></b></body></html>'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same as: 0c74d6c#r374548506

just check for 404 Page not found.

end

def test_undefinedfileformat
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use underscores to separate words

get '/users/nickname/hema?timezone=UTC&format=xtml'
assert_equal last_response.body, 'ERROR:The file doesnot exist'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect a status code 415 if the format does not exist

end
end
8 changes: 8 additions & 0 deletions RackApp/test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'test/unit'
require 'rack/test'

class TestBase < Test::Unit::TestCase
include Rack::Test::Methods
end