-
Notifications
You must be signed in to change notification settings - Fork 1
Rack app to display username and current time #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
306b9a5
23c5d66
5ba31a3
93e970e
0c74d6c
147aa44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' | ||
| end | ||
|
|
@@ -65,6 +65,7 @@ def initialize(app = nil) | |
| def call(env) | ||
| @app&.call(env) | ||
| req = Rack::Request.new(env) | ||
| puts env.inspect | ||
|
||
| timezone = req.params['timezone'] | ||
| format = req.params['format'] | ||
| nickname_match = req.path.match('/users/nickname/(\w+)') | ||
|
|
@@ -94,5 +95,3 @@ def response_format(format) | |
| end | ||
| end | ||
| end | ||
|
|
||
| Rack::Server.start(app: MyApp.new).new | ||
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name" : "<%= person.nickname%>", "time" : "<%= time.timezone %>" | ||
|
|
||
| } |
| 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> |
| 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' | ||
|
||
| puts @timezone = Time.zone.now | ||
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 |
There was a problem hiding this comment.
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