Skip to content
Browse files

refactored out HTTPGetStrategy and updated tests

  • Loading branch information...
1 parent d95599a commit f5d98707378d76b2ccc9e19ef96dec08b3be3667 @JosephJNK JosephJNK committed Mar 29, 2012
View
8 Makefile
@@ -1,10 +1,4 @@
test:
- ./node_modules/.bin/mocha --reporter list domain/scout/{models/*,services}/specs/*.coffee
-
-test-models:
- ./node_modules/.bin/mocha --reporter list domain/scout/models/*/specs/*.coffee
-
-test-services:
- ./node_modules/.bin/mocha --reporter list domain/scout/services/specs/*.coffee
+ ./node_modules/.bin/mocha --reporter list domain/scout/{{models/*,services}/specs,integration}/*.coffee
.PHONY: test
View
58 domain/scout/integration/getWithMaximumReassignments.spec.coffee
@@ -1,56 +1,34 @@
-Scout = require '../../Scout'
-FilteringStrategy = require '../../models/filteringStrategies/FilteringStrategy'
-NoFilteringStrategy = require '../../models/filteringStrategies/NoFilteringStrategy'
-FormattingStrategy = require '../../models/formattingStrategies/FormattingStrategy'
-EchoInputStrategy = require '../../models/retrievalStrategies/EchoInputStrategy'
-HTTPGetStrategy = require '../../models/retrievalStrategies/HTTPGetStrategy'
-NoFormattingStrategy = require '../../models/formattingStrategies/NoFormattingStrategy'
-http = require('http')
-
-pageResponse = "Retrieval was successful"
-server = {}
-serverPort = 9876
-
-before =>
- onRequest = (request, response) ->
- response.writeHead 200, "Content-Type": "text/plain"
- response.write pageResponse
- response.end()
-
- tryNextPort = (error)=>
- if error.code == 'EADDRINUSE'
- if serverPort < 9900
- serverPort++
- server.listen serverPort, http.INADDR_ANY
-
- server = http.createServer(onRequest)
- server.on 'error', tryNextPort
- server.listen serverPort, http.INADDR_ANY
+Scout = require '../Scout'
+FilteringStrategy = require '../models/filteringStrategies/FilteringStrategy'
+NoFilteringStrategy = require '../models/filteringStrategies/NoFilteringStrategy'
+FormattingStrategy = require '../models/formattingStrategies/FormattingStrategy'
+EchoInputStrategy = require '../models/retrievalStrategies/EchoInputStrategy'
+NoFormattingStrategy = require '../models/formattingStrategies/NoFormattingStrategy'
+CdrStrategy = require './mocks/CdrStrategy'
+ArrayToStringStrategy = require './mocks/ArrayToStringStrategy'
+EveryOtherStrategy = require './mocks/EveryOtherStrategy'
+http = require('http')
require 'should'
describe 'When constructors, accessors, and arguments are all used', ->
- describe '#get', ->
+ describe '#scout', ->
it 'should behave appropriately for the most recent assignments', (done)->
- class ArrayToString extends FormattingStrategy
- applyFormat: (data) ->
- data.join()
- class EveryOtherStrategy extends FilteringStrategy
- applyFilter: (data) ->
- (x for x in data[0..data.length] by 2)
class Return2Strategy extends FilteringStrategy
- applyFilter: (data) ->
+ applyFilter: (data)->
return 2
- scout = new Scout(EchoInputStrategy, ArrayToString, EveryOtherStrategy)
- scout.setRetriever(HTTPGetStrategy)
+ scout = new Scout(CdrStrategy, ArrayToStringStrategy, EveryOtherStrategy)
+ scout.setRetriever(EchoInputStrategy)
scout.setFormatter(NoFormattingStrategy)
scout.setFilterer(Return2Strategy)
+ output = "This should be output"
+
expectedResults = (results)=>
- results.should.eql pageResponse
+ results.should.eql output
done()
- scout.get "http://localhost:#{serverPort}", expectedResults, NoFilteringStrategy
+ scout.get output, expectedResults, NoFilteringStrategy
View
39 domain/scout/integration/getWithMultipleAgents.spec.coffee
@@ -1,42 +1,21 @@
-Scout = require '../../Scout'
-EchoInputStrategy = require '../../models/retrievalStrategies/EchoInputStrategy'
-http = require 'http'
+Scout = require '../Scout'
+CdrStrategy = require './mocks/CdrStrategy'
require 'should'
-pageResponse = "Retrieval was successful"
-server = {}
-serverPort = 9876
-
-before =>
- onRequest = (request, response) ->
- response.writeHead 200, "Content-Type": "text/plain"
- response.write pageResponse
- response.end()
-
- tryNextPort = (error)=>
- if error.code == 'EADDRINUSE'
- if serverPort < 9900
- serverPort++
- server.listen serverPort, http.INADDR_ANY
-
- server = http.createServer(onRequest)
- server.on 'error', tryNextPort
- server.listen serverPort, http.INADDR_ANY
-
-
describe 'When using multiple scouts with different strategies', ->
describe '#get', ->
it "should not be affected by other scouts", (done) ->
- httpScout = new Scout()
echoScout = new Scout()
- echoScout.setRetriever EchoInputStrategy
+ cdrScout = new Scout()
+ cdrScout.setRetriever CdrStrategy
dataToEcho = "Repeat me"
+ dataToCdr = [1..10]
- httpScout.get "http://localhost:#{serverPort}/", (httpData)=>
- httpData.should.eql pageResponse
- echoScout.get dataToEcho, (echoData)=>
- echoData.should.eql dataToEcho
+ echoScout.get dataToEcho, (echoData)=>
+ echoData.should.eql dataToEcho
+ cdrScout.get dataToCdr, (cdrData)=>
+ cdrData.should.eql [2..10]
done()
View
7 domain/scout/integration/mocks/ArrayToStringStrategy.coffee
@@ -0,0 +1,7 @@
+FormattingStrategy = require '../../models/formattingStrategies/FormattingStrategy'
+
+class ArrayToStringStrategy extends FormattingStrategy
+ applyFormat: (data) ->
+ data.join()
+
+module.exports = ArrayToStringStrategy
View
7 domain/scout/integration/mocks/CdrStrategy.coffee
@@ -0,0 +1,7 @@
+RetrievalStrategy = require '../../models/retrievalStrategies/RetrievalStrategy'
+
+class CdrStrategy extends RetrievalStrategy
+ retrieve: (location, callback) ->
+ callback location[1...location.length]
+
+module.exports = CdrStrategy
View
7 domain/scout/integration/mocks/EveryOtherStrategy.coffee
@@ -0,0 +1,7 @@
+FilteringStrategy = require '../../models/filteringStrategies/FilteringStrategy'
+
+class EveryOtherStrategy extends FilteringStrategy
+ applyFilter: (data) ->
+ (x for x in data[0..data.length] by 2)
+
+module.exports = EveryOtherStrategy
View
1 domain/scout/models/retrievalStrategies/EchoInputStrategy.coffee
@@ -1,6 +1,5 @@
RetrievalStrategy = require './RetrievalStrategy'
-#This is for mocking input during testing
class EchoInputStrategy extends RetrievalStrategy
retrieve: (location, callback)->
callback location
View
79 domain/scout/models/retrievalStrategies/specs/HTTPGetStrategy.spec.coffee
@@ -1,79 +0,0 @@
-HTTPGetStrategy = require '../HTTPGetStrategy'
-require 'should'
-http = require('http')
-url = require('url')
-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 pageFoundResponse
- else if pathname == "/error310"
- response.writeHead 310, "Content-Type": "text/plain"
- response.write "this is an error"
- else
- response.writeHead 404, "Content-Type": "text/plain"
- response.write "page not found"
- response.end()
-
- tryNextPort = (error)=>
- if error.code == 'EADDRINUSE'
- if serverPort < 9900
- serverPort++
- server.listen serverPort, http.INADDR_ANY
-
- server = http.createServer(onRequest)
- server.on 'error', tryNextPort
- server.listen serverPort, http.INADDR_ANY
-
-
-describe 'HTTPGetStrategy', ->
- describe '#get', ->
- it 'should get the contents of an http location', (done)->
- httpGetter.retrieve "http://localhost:#{serverPort}/valid", (data)->
- 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.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.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()
-
View
16 domain/scout/services/specs/getWhenConstructedWithStrategies.spec.coffee
@@ -1,29 +1,30 @@
Scout = require '../../Scout'
FilteringStrategy = require '../../models/filteringStrategies/FilteringStrategy'
FormattingStrategy = require '../../models/formattingStrategies/FormattingStrategy'
+RetrievalStrategy = require '../../models/retrievalStrategies/RetrievalStrategy'
EchoInputStrategy = require '../../models/retrievalStrategies/EchoInputStrategy'
NoFormattingStrategy = require '../../models/formattingStrategies/NoFormattingStrategy'
+CdrStrategy = require './mocks/CdrStrategy'
+EveryOtherStrategy = require './mocks/EveryOtherStrategy'
+ArrayToStringStrategy = require './mocks/ArrayToStringStrategy'
require 'should'
describe 'When constructed with a custom retrieval technique', ->
describe '#get', ->
it 'should return results', (done)->
data = [0..10]
- connectionScout = new Scout(EchoInputStrategy)
+ connectionScout = new Scout(CdrStrategy)
connectionScout.get data, (results)=>
- results.should.eql data
+ results.should.eql [1..10]
done()
describe 'When constructed with a custom formatter', ->
describe '#get', ->
it 'should reformat results', (done)->
data = [0..10]
- class ArrayToString extends FormattingStrategy
- applyFormat: (data) ->
- data.join()
- formatScout = new Scout(EchoInputStrategy, ArrayToString)
+ formatScout = new Scout(EchoInputStrategy, ArrayToStringStrategy)
formatScout.get data, (results)=>
results.should.eql data.join()
@@ -32,9 +33,6 @@ describe 'When constructed with a custom formatter', ->
describe 'When constructed with a custom filter', ->
describe '#get', ->
it 'should apply filter to results', (done)->
- class EveryOtherStrategy extends FilteringStrategy
- applyFilter: (data) ->
- (x for x in data[0..data.length] by 2)
filterScout = new Scout(EchoInputStrategy, NoFormattingStrategy, EveryOtherStrategy)
data = (x for x in [0..10])
View
20 domain/scout/services/specs/getWhenStrategiesChanged.spec.coffee
@@ -1,7 +1,9 @@
Scout = require '../../Scout'
FilteringStrategy = require '../../models/filteringStrategies/FilteringStrategy'
FormattingStrategy = require '../../models/formattingStrategies/FormattingStrategy'
-EchoInputStrategy = require '../../models/retrievalStrategies/EchoInputStrategy'
+CdrStrategy = require './mocks/CdrStrategy'
+EveryOtherStrategy = require './mocks/EveryOtherStrategy'
+ArrayToStringStrategy = require './mocks/ArrayToStringStrategy'
require 'should'
@@ -11,22 +13,19 @@ describe 'When retrieval technique is changed', ->
data = [0..10]
connectionScout = new Scout()
- connectionScout.setRetriever(EchoInputStrategy)
+ connectionScout.setRetriever(CdrStrategy)
connectionScout.get data, (results)=>
- results.should.eql data
+ results.should.eql [1..10]
done()
describe 'When formatter is changed', ->
describe '#get', ->
it 'should reformat results', (done)->
data = [0..10]
- class ArrayToString extends FormattingStrategy
- applyFormat: (data) ->
- data.join()
- formatScout = new Scout(EchoInputStrategy)
+ formatScout = new Scout()
- formatScout.setFormatter(ArrayToString)
+ formatScout.setFormatter(ArrayToStringStrategy)
formatScout.get data, (results)=>
results.should.eql data.join()
@@ -35,10 +34,7 @@ describe 'When formatter is changed', ->
describe 'When filterer is changed', ->
describe '#get', ->
it 'should apply filter to results', (done)->
- class EveryOtherStrategy extends FilteringStrategy
- applyFilter: (data) ->
- (x for x in data[0..data.length] by 2)
- filterScout = new Scout(EchoInputStrategy)
+ filterScout = new Scout()
filterScout.setFilterer(EveryOtherStrategy)
View
7 domain/scout/services/specs/getWithDefaultConstructor.spec.coffee
@@ -3,8 +3,9 @@ require 'should'
describe 'Using Default Constructor', ->
describe '#get', ->
- it 'should echo what was done',(done)->
+ it 'should echo its input',(done)->
scout = new Scout()
- scout.get "boogah", (data)=>
- data.should.equal "boogah"
+ response = "I'm the response"
+ scout.get response, (data)=>
+ data.should.eql response
done()
View
7 domain/scout/services/specs/getWithStrategyArgument.spec.coffee
@@ -1,16 +1,13 @@
Scout = require '../../Scout'
FilteringStrategy = require '../../models/filteringStrategies/FilteringStrategy'
-EchoInputStrategy = require '../../models/retrievalStrategies/EchoInputStrategy'
+EveryOtherStrategy = require './mocks/EveryOtherStrategy'
require 'should'
describe 'Given a filtering strategy argument', ->
describe '#get', ->
it 'should get data and filter it according to the strategy',(done)->
- scout = new Scout(EchoInputStrategy)
+ scout = new Scout()
data = [0..10]
- class EveryOtherStrategy extends FilteringStrategy
- applyFilter: (data) ->
- (x for x in data[0..data.length] by 2)
callback = (result) =>
result.should.not.eql data
View
7 domain/scout/services/specs/mocks/ArrayToStringStrategy.coffee
@@ -0,0 +1,7 @@
+FormattingStrategy = require '../../../models/formattingStrategies/FormattingStrategy'
+
+class ArrayToStringStrategy extends FormattingStrategy
+ applyFormat: (data) ->
+ data.join()
+
+module.exports = ArrayToStringStrategy
View
7 domain/scout/services/specs/mocks/CdrStrategy.coffee
@@ -0,0 +1,7 @@
+RetrievalStrategy = require '../../../models/retrievalStrategies/RetrievalStrategy'
+
+class CdrStrategy extends RetrievalStrategy
+ retrieve: (location, callback) ->
+ callback location[1...location.length]
+
+module.exports = CdrStrategy
View
7 domain/scout/services/specs/mocks/EveryOtherStrategy.coffee
@@ -0,0 +1,7 @@
+FilteringStrategy = require '../../../models/filteringStrategies/FilteringStrategy'
+
+class EveryOtherStrategy extends FilteringStrategy
+ applyFilter: (data) ->
+ (x for x in data[0..data.length] by 2)
+
+module.exports = EveryOtherStrategy

0 comments on commit f5d9870

Please sign in to comment.
Something went wrong with that request. Please try again.