Compare commits

..

3 Commits

Author SHA1 Message Date
Adam Townsend 144731062b added URL validation to webpage part 2023-10-12 23:42:52 -07:00
Adam Townsend 72f3e03edd added tests for invalid URL entries
+ URL that returns a 404
+ URL where the domain does not resolve with DNS
+ URL that is not properly formatted
2023-10-12 23:42:04 -07:00
Adam Townsend b08693e866 remove the puts, don't print out the test result 2023-10-12 23:41:38 -07:00
3 changed files with 39 additions and 1 deletions

14
app.rb
View File

@ -34,6 +34,20 @@ class App < Roda
flash['message'] = "Please enter a valid URL"; flash['message'] = "Please enter a valid URL";
r.redirect '/' r.redirect '/'
end end
begin
OpenURI.open_uri(url)
rescue URI::BadURIError
flash['message'] = "Invalid URL"
r.redirect '/'
rescue OpenURI::HTTPError
flash['message'] = "URL not found"
r.redirect '/'
rescue SocketError => e
flash['message'] = "URL does not resolve"
r.redirect '/'
end
if links.filter(:url => url).first.nil? 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)

View File

@ -63,7 +63,7 @@ Then('I should be redirected to {string}') do |string|
end end
Then('The status code should be {int}') do |code| Then('The status code should be {int}') do |code|
puts page.status_code.should eq(code) page.status_code.should eq(code)
end end

View File

@ -26,3 +26,27 @@ Feature: Submit
And I click the "Submit" button And I click the "Submit" button
Then I should be on "/create" page Then I should be on "/create" page
And I should see the message "Link exists" And I should see the message "Link exists"
@db-test
Scenario: Submitting the form with a URL that is 404
Given I visit the "/" page
When I type "http://google.com/example" in the "url" field
And I click the "Submit" button
Then I should be on "/" page
And I should see the message "URL not found"
@db-test
Scenario: Submitting the form with a URL that does not resolve
Given I visit the "/" page
When I type "http://bad.tld" in the "url" field
And I click the "Submit" button
Then I should be on "/" page
And I should see the message "URL does not resolve"
@db-test
Scenario: Submitting the form with an invalid URL
Given I visit the "/" page
When I type "not-an-url" in the "url" field
And I click the "Submit" button
Then I should be on "/" page
And I should see the message "Invalid URL"