Compare commits

..

2 Commits

Author SHA1 Message Date
Adam Townsend 5bce31baf2 refactored missing link scenario to make more sense
+ 404 code, stay on the current page, and load the form
+ also simplified the logic for the create action if the link doesn't
  already exist
2023-10-07 21:11:12 -07:00
Adam Townsend c7fee27623 update for missing link scenario
+ if the link isn't found, don't redirect it, keep it on the same page
+ it should have a status code of 404
+ it should have the form fields as well

+ added page status check
2023-10-07 21:10:16 -07:00
3 changed files with 18 additions and 7 deletions

11
app.rb
View File

@ -19,11 +19,10 @@ class App < Roda
r.get String do | url_code | r.get String do | url_code |
link = links.filter(:code => url_code) link = links.filter(:code => url_code)
if link.first.nil? r.redirect link.first[:url] unless link.first.nil?
flash['message'] = "Link #{url_code} doesn't exist" @message = "Link #{url_code} doesn't exist"
r.redirect '/' response.status = 404
end view :home
r.redirect link.first[:url]
end end
r.post "create" do r.post "create" do
@ -32,7 +31,7 @@ class App < Roda
flash['message'] = "Please enter a valid URL"; flash['message'] = "Please enter a valid URL";
r.redirect '/' r.redirect '/'
end end
if nil == links.filter(:url => url).first if links.filter(:url => url).first.nil?
code = SecureRandom.urlsafe_base64 4 code = SecureRandom.urlsafe_base64 4
links.insert(url: url, code: code) links.insert(url: url, code: code)
@message = "Link created" @message = "Link created"

View File

@ -42,5 +42,8 @@ Feature: Homepage
Scenario: Accessing a nonexistent URL code Scenario: Accessing a nonexistent URL code
Given I visit the "/aaaaaa" location Given I visit the "/aaaaaa" location
Then I should be on "/" page Then I should be on "/aaaaaa" page
And The status code should be 404
And I should see the message "Link aaaaaa doesn't exist" And I should see the message "Link aaaaaa doesn't exist"
And I should see a form field "url"
And I should see a "Submit" button

View File

@ -3,6 +3,7 @@ Before('@db-test') do
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links] @links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
end end
# GIVEN # GIVEN
Given('I visit the {string} page') do |string| Given('I visit the {string} page') do |string|
@ -17,6 +18,7 @@ Given('A link already exists with the url {string} and code {string}') do |url,
@links.insert(url: url, code: code) @links.insert(url: url, code: code)
end end
# WHEN # WHEN
When('I click the {string} button') do |string| When('I click the {string} button') do |string|
@ -31,6 +33,7 @@ When('I visit the {string} location') do |string|
visit string visit string
end end
# THEN # THEN
Then('I should see text {string}') do |string| Then('I should see text {string}') do |string|
@ -58,6 +61,12 @@ Then('I should be redirected to {string}') do |string|
location = actual.index(string) location = actual.index(string)
location.should equal(0) location.should equal(0)
end end
Then('The status code should be {int}') do |code|
puts page.status_code.should eq(code)
end
# AFTER # AFTER
After('@db-test') do After('@db-test') do