Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added minor and inadequate URL validation to HTTPGetStrategy

  • Loading branch information...
commit 530a33ec1d3732e851396d7809d6cd8e573e445e 1 parent 3f0f481
@JosephJNK JosephJNK authored
View
7 domain/scout/models/retrievalStrategies/HTTPGetStrategy.coffee
@@ -2,9 +2,16 @@ RetrievalStrategy = require './RetrievalStrategy'
http = require 'http'
url = require 'url'
+validateURLElements = (elements) ->
+ if !elements.hostname? or elements.protocol != "http:"
+ console.log "Throwing!"
+ throw new Error "Invalid URL"
+
class HTTPGetStrategy extends RetrievalStrategy
retrieve: (location, callback) ->
locationElements = url.parse location
+ console.log "host: #{locationElements.hostname} protocol: #{locationElements.protocol}"
+ validateURLElements locationElements
options = {
hostname: locationElements.hostname
port: locationElements.port
View
34 domain/scout/models/retrievalStrategies/specs/HTTPGetStrategy.spec.coffee
@@ -7,12 +7,14 @@ httpGetter = new HTTPGetStrategy()
server = {}
serverPort = 9876
+pageFoundResponse = "page was found"
+
before ->
onRequest = (request, response) ->
pathname = url.parse(request.url).pathname
if pathname == "/valid"
response.writeHead 200, "Content-Type": "text/plain"
- response.write "page was found"
+ response.write pageFoundResponse
else if pathname == "/error310"
response.writeHead 310, "Content-Type": "text/plain"
response.write "this is an error"
@@ -36,18 +38,42 @@ describe 'HTTPGetStrategy', ->
describe '#get', ->
it 'should get the contents of an http location', (done)->
httpGetter.retrieve "http://localhost:#{serverPort}/valid", (data)->
- data.should.equal "page was found"
+ data.should.eql pageFoundResponse
done()
describe '#get', ->
it 'should return an error code if an error occurs during the request', (done)->
httpGetter.retrieve "http://localhost:#{serverPort}/error310", (data)->
- data.should.equal "310"
+ data.should.eql "310"
done()
describe '#get', ->
it 'should return 404 if the resource is not found', (done)->
httpGetter.retrieve "http://localhost:#{serverPort}/invalid", (data)->
- data.should.equal "404"
+ data.should.eql "404"
done()
+ describe '#get', ->
+ it 'should throw an exception if given an invalid url', (done)->
+ invalidRequest1 = ()->
+ httpGetter.retrieve "this is not a valid URL", (data)->
+ invalidRequest1.should.throw("Invalid URL")
+ invalidRequest2 = ()->
+ httpGetter.retrieve "http:www.google.com", (data)->
+ invalidRequest2.should.throw("Invalid URL")
+ done()
+
+ describe '#get', ->
+ it 'should throw an exception if url has no protocol', (done)->
+ invalidRequest = ()->
+ httpGetter.retrieve "www.google.com", (data)->
+ invalidRequest.should.throw("Invalid URL")
+ done()
+
+ describe '#get', ->
+ it 'should throw an exception if url protocol is not http', (done)->
+ invalidRequest = ()->
+ httpGetter.retrieve "ftp://www.google.com", (data)->
+ invalidRequest.should.throw("Invalid URL")
+ done()
+
Please sign in to comment.
Something went wrong with that request. Please try again.