Merge pull request 'invalid-urls' (#18) from invalid-urls into master

Reviewed-on: #18
pull/21/head
bucky 2023-10-07 21:13:53 -07:00
commit e6cc0959a4
3 changed files with 23 additions and 3 deletions

9
app.rb
View File

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

View File

@ -39,3 +39,11 @@ Feature: Homepage
Given A link already exists with the url "https://google.com" and code "aaaaaa"
When I visit the "/aaaaaa" location
Then I should be redirected to "https://google.com"
Scenario: Accessing a nonexistent URL code
Given I visit the "/aaaaaa" location
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 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]
end
# GIVEN
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)
end
# WHEN
When('I click the {string} button') do |string|
@ -31,6 +33,7 @@ When('I visit the {string} location') do |string|
visit string
end
# THEN
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.should equal(0)
end
Then('The status code should be {int}') do |code|
puts page.status_code.should eq(code)
end
# AFTER
After('@db-test') do