Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 1.5.3

Tested on Node 0.8.1
  • Loading branch information...
commit 6ef04add8d6c10fefa2e8d4bfd82799d4eb7f4ec 1 parent 59013d7
@assaf authored
View
13 CHANGELOG.md
@@ -1,3 +1,16 @@
+## Version 1.6.0 2012-10-16
+
+Improved logging options:
+- Replay will now emit errors to the console unless otherwise told
+- You can tell Replay to not emit errors to console by setting
+ Replay.silent = true
+- You can also tell Replay to emit errors an logs messages elsewhere by setting
+ Replay.logger (defaults to console)
+- You can listen to all events via Replay.on("error", function(error) { })
+
+No deprecation messages on Node 0.8
+
+
## Version 1.5.3 2012-06-30
Removed engine dependency. Works on 0.8.1 now.
View
16 lib/replay/catalog.coffee
@@ -3,15 +3,18 @@ File = require("fs")
Path = require("path")
Matcher = require("./matcher")
+exists = File.exists || Path.exists
+existsSync = File.existsSync || Path.existsSync
+
mkdir = (pathname, callback)->
- Path.exists pathname, (exists)->
- if exists
+ exists pathname, (found)->
+ if found
callback null
return
parent = Path.dirname(pathname)
- Path.exists parent, (exists)->
- if exists
+ exists parent, (found)->
+ if found
File.mkdir pathname, callback
else
mkdir parent, ->
@@ -35,7 +38,7 @@ class Catalog
# Start by looking for directory and loading each of the files.
pathname = "#{@basedir}/#{host}"
- unless Path.existsSync(pathname)
+ unless existsSync(pathname)
return
stat = File.statSync(pathname)
if stat.isDirectory()
@@ -59,7 +62,8 @@ class Catalog
uid = +new Date
tmpfile = "/tmp/node-replay.#{uid}"
pathname = "#{@basedir}/#{host}"
- console.log "Creating #{pathname}"
+ logger = request.replay.logger
+ logger.log "Creating #{pathname}"
mkdir pathname, (error)->
return callback error if error
filename = "#{pathname}/#{uid}"
View
2  lib/replay/index.coffee
@@ -14,7 +14,7 @@ HTTP.request = (options, callback)->
if options && options.headers && options.headers["Upgrade"] == "websocket"
return httpRequest(options, callback)
# Proxy request
- request = new ProxyRequest(options, Replay.chain.start)
+ request = new ProxyRequest(options, Replay, Replay.chain.start)
if callback
request.once "response", (response)->
callback response
View
17 lib/replay/logger.coffee
@@ -1,14 +1,23 @@
URL = require("url")
+
# Simple proxy that spits all request URLs to the console if the debug settings is true.
#
# Example
-# replay.use replay.logger(exports)
-# replay.debug = true
+# replay.use replay.logger(exports)
+# replay.debug = true
logger = (settings)->
return (request, callback)->
- if settings.debug
- console.log "Replay: Requesting #{request.method} #{URL.format(request.url)}"
+ replay = request.replay
+ logger = replay.logger
+ logger.log "Replay: Requesting #{request.method} #{URL.format(request.url)}"
+ request.on "response", (response)->
+ logger.log "Replay: Received #{response.statusCode} #{URL.format(request.url)}"
+ request.on "error", (error)->
+ unless replay.isIgnored(request.url.hostname)
+ replay.emit("error", error)
+
callback()
+ return
module.exports = logger
View
6 lib/replay/proxy.coffee
@@ -31,7 +31,7 @@ URL = require("url")
# HTTP client request that captures the request and sends it down the processing chain.
class ProxyRequest extends HTTP.ClientRequest
- constructor: (options = {}, @proxy)->
+ constructor: (options = {}, @replay, @proxy)->
@method = (options.method || "GET").toUpperCase()
[host, port] = (options.host || options.hostname).split(":")
@url = URL.parse("#{options.protocol || "http:"}//#{host || "localhost"}:#{options.port || port || 80}#{options.path || "/"}")
@@ -78,14 +78,14 @@ class ProxyRequest extends HTTP.ClientRequest
@ended = true
@proxy this, (error, captured)=>
+ # We're not asynchronous, but clients expect us to callback later on
process.nextTick =>
if error
@emit "error", error
else if captured
response = new ProxyResponse(captured)
@emit "response", response
- process.nextTick ->
- response.resume()
+ response.resume()
else
error = new Error("#{@method} #{URL.format(@url)} refused: not recording and no network access")
error.code = "ECONNREFUSED"
View
152 lib/replay/replay.coffee
@@ -1,78 +1,109 @@
# The Replay module holds global configution properties and methods.
-Catalog = require("./catalog")
-Chain = require("./chain")
-logger = require("./logger")
-passThrough = require("./pass_through")
-recorder = require("./recorder")
+Catalog = require("./catalog")
+Chain = require("./chain")
+{ EventEmitter } = require("events")
+logger = require("./logger")
+passThrough = require("./pass_through")
+recorder = require("./recorder")
# Supported modes.
MODES = ["bloody", "cheat", "record", "replay"]
-# Localhost servers. Pass requests directly to host, and route to 127.0.0.1.
-localhosts = { localhost: true }
-# Allowed servers. Allow network access to any servers listed here.
-allowed = { }
-# Ignored servers. Do not contact or record.
-ignored = { }
+# Instance properties:
+#
+# catalog - The catalog is responsible for loading pre-recorded responses
+# into memory, from where they can be replayed, and storing captured responses.
+#
+# chain - The proxy chain. Essentially an array of proxies through which
+# each request goes, from first to last. You generally don't need
+# to use this unless you decide to reconstruct your own chain.
+#
+# When adding new proxies, you probably want those executing ahead
+# of any existing proxies (certainly the pass-through proxy), so
+# you'll want to prepend them. The `use` method will prepend a
+# proxy to the chain.
+#
+# debug - Set this to true to dump more information to the console, or run
+# with DEBUG=true
+#
+# fixtures - Main directory for replay fixtures.
+#
+# logger - Logger to use (defaults to console)
+#
+# mode - The mode we're running in, one of:
+# bloody - Allow outbound HTTP requests, don't replay anything. Use this to
+# test your code against changes to 3rd party API.
+# cheat - Allow outbound HTTP requests, replay captured responses. This
+# mode is particularly useful when new code makes new requests, but
+# unstable yet and you don't want these requests saved.
+# record - Allow outbound HTTP requests, capture responses for future
+# replay. This mode allows you to capture and record new requests,
+# e.g. when adding tests or making code changes.
+# replay - Do not allow outbound HTTP requests, replay captured responses.
+# This is the default mode and the one most useful for running tests
+#
+# silent - If true do not emit errors to logger
+class Replay extends EventEmitter
+ constructor: (mode)->
+ unless ~MODES.indexOf(mode)
+ throw new Error("Unsupported mode '#{mode}', must be one of #{MODES.join(", ")}.")
+ @chain = new Chain()
+ @debug = !!process.env.DEBUG
+ @fixtures = null
+ @logger =
+ log: (message)=>
+ if @debug
+ console.log message
+ error: (message)=>
+ if @debug || !@silent
+ console.error message
+ @mode = mode
+ # localhost servers. pass requests directly to host, and route to 127.0.0.1.
+ @_localhosts = { localhost: true }
+ # allowed servers. allow network access to any servers listed here.
+ @_allowed = { }
+ # ignored servers. do not contact or record.
+ @_ignored = { }
+ @catalog = new Catalog(this)
+
+ # Automatically emit connection errors and such, also prevent process from failing.
+ @on "error", (error, url)=>
+ @logger.error "Replay: #{error.message || error}"
-Replay =
- # The proxy chain. Essentially an array of proxies through which each request goes, from first to last. You
- # generally don't need to use this unless you decide to reconstruct your own chain.
- #
- # When adding new proxies, you probably want those executing ahead of any existing proxies (certainly the pass-through
- # proxy), so you'll want to prepend them. The `use` method will prepend a proxy to the chain.
- chain: new Chain()
-
- # Set this to true to dump more information to the console, or run with DEBUG=true
- debug: !!process.env.DEBUG
-
- # Main directory for replay fixtures.
- fixtures: null
-
- # The mode we're running in, one of:
- # bloody - Allow outbound HTTP requests, don't replay anything. Use this to test your code against changes to 3rd
- # party API.
- # cheat - Allow outbound HTTP requests, replay captured responses. This mode is particularly useful when new code
- # makes new requests, but unstable yet and you don't want these requests saved.
- # record - Allow outbound HTTP requests, capture responses for future replay. This mode allows you to capture and
- # record new requests, e.g. when adding tests or making code changes.
- # replay - Do not allow outbound HTTP requests, replay captured responses. This is the default mode and the one most
- # useful for running tests
- mode: process.env.REPLAY || "replay"
# Addes a proxy to the beginning of the processing chain, so it executes ahead of any existing proxy.
#
# Example
# replay.use replay.logger()
use: (proxy)->
- Replay.chain.prepend proxy
+ @chain.prepend(proxy)
# Allow network access to this host.
allow: (hosts...)->
for host in hosts
- allowed[host] = true
- delete ignored[host]
- delete localhosts[host]
+ @_allowed[host] = true
+ delete @_ignored[host]
+ delete @_localhosts[host]
# True if this host is allowed network access.
isAllowed: (host)->
- return !!allowed[host]
+ return !!@_allowed[host]
# Ignore network access to this host.
ignore: (hosts...)->
for host in hosts
- ignored[host] = true
- delete allowed[host]
- delete localhosts[host]
+ @_ignored[host] = true
+ delete @_allowed[host]
+ delete @_localhosts[host]
# True if this host is on the ignored list.
isIgnored: (host)->
- return !!ignored[host]
+ return !!@_ignored[host]
# Treats this host as localhost: requests are routed directory to 127.0.0.1, no replay. Useful when you want to send
# requests to the test server using its production host name.
@@ -81,18 +112,16 @@ Replay =
# replay.localhost "www.example.com"
localhost: (hosts...)->
for host in hosts
- localhosts[host] = true
- delete allowed[host]
- delete ignored[host]
+ @_localhosts[host] = true
+ delete @_allowed[host]
+ delete @_ignored[host]
# True if this host should be treated as localhost.
isLocalhost: (host)->
- return !!localhosts[host]
+ return !!@_localhosts[host]
-# The catalog is responsible for loading pre-recorded responses into memory, from where they can be replayed, and
-# storing captured responses.
-Replay.catalog = new Catalog(Replay)
+replay = new Replay(process.env.REPLAY || "replay")
# The default processing chain (from first to last):
@@ -100,16 +129,17 @@ Replay.catalog = new Catalog(Replay)
# - Log request to console is `deubg` is true
# - Replay recorded responses
# - Pass through requests in bloody and cheat modes
-Replay.use passThrough (request)->
- return Replay.isAllowed(request.url.hostname) || (Replay.mode == "cheat" && !Replay.isIgnored(request.url.hostname))
-Replay.use recorder(Replay)
-Replay.use logger(Replay)
-Replay.use passThrough (request)->
- return Replay.isLocalhost(request.url.hostname) || Replay.mode == "bloody"
-
+passWhenBloodyOrCheat = (request)->
+ return replay.isAllowed(request.url.hostname) ||
+ (replay.mode == "cheat" && !replay.isIgnored(request.url.hostname))
+passToLocalhost = (request)->
+ return replay.isLocalhost(request.url.hostname) ||
+ replay.mode == "bloody"
-unless ~MODES.indexOf(Replay.mode)
- throw new Error("Unsupported mode '#{Replay.mode}', must be one of #{MODES.join(", ")}.")
+replay.use passThrough(passWhenBloodyOrCheat)
+replay.use recorder(replay)
+replay.use logger(replay)
+replay.use passThrough(passToLocalhost)
-module.exports = Replay
+module.exports = replay
View
42 test/helpers.coffee
@@ -1,6 +1,6 @@
# NOTES:
# All requests using a hostname are routed to 127.0.0.1
-# Port 3001 has a live server, see below for paths and responses
+# Port 3004 has a live server, see below for paths and responses
# Port 3002 has no server, connections will be refused
# Port 3443 has a live https server
@@ -12,9 +12,16 @@ File = require("fs")
Async = require("async")
+HTTP_PORT = 3004
+HTTPS_PORT = 3443
+INACTIVE_PORT = 3002
+
+
# Directory to load fixtures from.
Replay.fixtures = "#{__dirname}/fixtures"
+Replay.silent = true
+
# Redirect HTTP requests to pass-through domain
original_lookup = DNS.lookup
@@ -65,25 +72,18 @@ ssl_server.get "/500", (req, res)->
setup = (callback)->
Async.parallel [
(done)->
- server.listen 3001, done
+ server.listen HTTP_PORT, done
(done)->
- ssl_server.listen 3443, done
+ ssl_server.listen HTTPS_PORT, done
], callback
-
- return
-
- if server._connected
- callback null
- return
- server.on "listening", callback
- unless server._connecting
- server._connecting = true
- server.listen 3001, ->
- server._connected = true
-
-
-exports.assert = require("assert")
-exports.setup = setup
-exports.HTTP = require("http")
-exports.HTTPS = require("https")
-exports.Replay = Replay
+
+
+module.exports =
+ assert: require("assert")
+ setup: setup
+ HTTP: require("http")
+ HTTPS: require("https")
+ HTTP_PORT: HTTP_PORT
+ HTTPS_PORT: HTTPS_PORT
+ INACTIVE_PORT: INACTIVE_PORT
+ Replay: Replay
View
14 test/pass_through_test.coffee
@@ -1,4 +1,4 @@
-{ assert, setup, HTTP, HTTPS, Replay } = require("./helpers")
+{ assert, setup, HTTP, HTTPS, Replay, HTTP_PORT, HTTPS_PORT } = require("./helpers")
# First batch is testing requests that pass through to the server, no recording/replay.
@@ -8,7 +8,7 @@ describe "Pass through", ->
before setup
- # Send request to the live server on port 3001 and check the responses.
+ # Send request to the live server and check the responses.
describe "bloody", ->
before ->
Replay.mode = "bloody"
@@ -17,7 +17,7 @@ describe "Pass through", ->
response = null
before (done)->
- request = HTTP.get(hostname: "pass-through", port: 3001)
+ request = HTTP.get(hostname: "pass-through", port: HTTP_PORT)
request.on "response", (_)->
response = _
response.body = ""
@@ -42,7 +42,7 @@ describe "Pass through", ->
response = null
before (done)->
- request = HTTP.get(hostname: "pass-through", port: 3001, (_)->
+ request = HTTP.get(hostname: "pass-through", port: HTTP_PORT, (_)->
response = _
response.body = ""
response.on "data", (chunk)->
@@ -70,7 +70,7 @@ describe "Pass through", ->
response = null
before (done)->
- request = HTTPS.get(hostname: "pass-through", port: 3443, (_)->
+ request = HTTPS.get(hostname: "pass-through", port: HTTPS_PORT, (_)->
response = _
response.body = ""
response.on "data", (chunk)->
@@ -90,7 +90,7 @@ describe "Pass through", ->
it "should return response body", ->
assert.deepEqual response.body, "Success!"
- # Send request to the live server on port 3001, but this time network connection disabled.
+ # Send request to the live server, but this time network connection disabled.
describe "replay", ->
before ->
Replay.mode = "replay"
@@ -99,7 +99,7 @@ describe "Pass through", ->
error = null
before (done)->
- request = HTTP.get(hostname: "pass-through", port: 3001)
+ request = HTTP.get(hostname: "pass-through", port: HTTP_PORT)
request.on "error", (_)->
error = _
done()
View
211 test/replay_test.coffee
@@ -1,4 +1,4 @@
-{ assert, setup, HTTP, HTTPS, Replay } = require("./helpers")
+{ assert, setup, HTTP, HTTPS, Replay, HTTP_PORT, HTTPS_PORT, INACTIVE_PORT } = require("./helpers")
File = require("fs")
Request = require("request")
@@ -7,183 +7,155 @@ Request = require("request")
describe "Replay", ->
- # Send responses to non-existent server on port 3002, expect replayed responses from fixtures.
+ # Send responses to non-existent server on inactive port, expect replayed responses from fixtures.
describe "matching URL", ->
before ->
Replay.mode = "replay"
describe "listeners", ->
- response = null
-
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, path: "/weather?c=94606")
- request.on "response", (_)->
- response = _
- response.on "end", done
- request.on "error", done
+ HTTP.get(hostname: "example.com", port: INACTIVE_PORT, path: "/weather?c=94606", (@response)=>
+ done()
+ ).on("error", done)
it "should return HTTP version", ->
- assert.equal response.httpVersion, "1.1"
+ assert.equal @response.httpVersion, "1.1"
it "should return status code", ->
- assert.equal response.statusCode, 200
+ assert.equal @response.statusCode, 200
it "should return response headers", ->
- assert.deepEqual response.headers, { "content-type": "text/html", "date": "Tue, 29 Nov 2011 03:12:15 GMT" }
+ assert.deepEqual @response.headers, { "content-type": "text/html", "date": "Tue, 29 Nov 2011 03:12:15 GMT" }
it "should return response trailers", ->
- assert.deepEqual response.trailers, { }
+ assert.deepEqual @response.trailers, { }
describe "callback", ->
- response = null
-
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, path: "/weather?c=94606")
- request.on "response", (_)->
- response = _
+ HTTP.get(hostname: "example.com", port: INACTIVE_PORT, path: "/weather?c=94606", (@response)=>
done()
- request.on "error", done
+ ).on("error", done)
it "should return HTTP version", ->
- assert.equal response.httpVersion, "1.1"
+ assert.equal @response.httpVersion, "1.1"
it "should return status code", ->
- assert.equal response.statusCode, 200
+ assert.equal @response.statusCode, 200
it "should return response headers", ->
- assert.deepEqual response.headers, { "content-type": "text/html", "date": "Tue, 29 Nov 2011 03:12:15 GMT" }
+ assert.deepEqual @response.headers, { "content-type": "text/html", "date": "Tue, 29 Nov 2011 03:12:15 GMT" }
it "should return response trailers", ->
- assert.deepEqual response.trailers, { }
+ assert.deepEqual @response.trailers, { }
describe "matching an https url", ->
- response = null
-
- before (done)->
+ before ->
Replay.mode = "replay"
-
- request = HTTPS.get(hostname: "example.com", port: 3443, path: "/minimal")
- request.on "response", (_)->
- response = _
+ before (done)->
+ HTTPS.get(hostname: "example.com", port: HTTPS_PORT, path: "/minimal", (@response)=>
done()
- request.on "error", done
+ ).on("error", done)
it "should return HTTP version", ->
- assert.equal response.httpVersion, "1.1"
+ assert.equal @response.httpVersion, "1.1"
it "should return status code", ->
- assert.equal response.statusCode, 200
+ assert.equal @response.statusCode, 200
describe "matching a regexp", ->
- body = null
-
before ->
Replay.mode = "replay"
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, method: "get", path: "/regexp", (response)->
- body = ""
- response.on "data", (chunk)->
- body += chunk
+ HTTP.get(hostname: "example.com", port: INACTIVE_PORT, method: "get", path: "/regexp", (response)=>
+ @body = ""
+ response.on "data", (chunk)=>
+ @body += chunk
response.on "end", done
- )
- request.on "error", done
+ ).on("error", done)
it "should match the right fixture", ->
- assert.equal body, "regexp"
+ assert.equal @body, "regexp"
describe "matching a regexp url with flags", ->
- body = null
-
before ->
Replay.mode = "replay"
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, method: "get", path: "/aregexp2", (response)->
- body = ""
- response.on "data", (chunk)->
- body += chunk
+ HTTP.get(hostname: "example.com", port: INACTIVE_PORT, method: "get", path: "/aregexp2", (response)=>
+ @body = ""
+ response.on "data", (chunk)=>
+ @body += chunk
response.on "end", done
- )
- request.on "error", done
+ ).on("error", done)
it "should match a fixture", ->
- assert.equal body, "Aregexp2"
+ assert.equal @body, "Aregexp2"
describe "recording multiple of the same header", ->
- fixture_path = null
-
before setup
before ->
Replay.mode = "record"
+ @fixturesDir = "#{__dirname}/fixtures/127.0.0.1:#{HTTP_PORT}"
before (done)->
- request = HTTP.get(hostname: "127.0.0.1", port: 3001, path: "/set-cookie", (response)->
- response.on "end", done
- )
- request.on "error", done
+ HTTP.get(hostname: "127.0.0.1", port: HTTP_PORT, path: "/set-cookie", (response)->
+ response.on("end", done)
+ ).on("error", done)
it "should create a fixture with multiple set-cookie headers", ->
- fixture_path = "#{__dirname}/fixtures/127.0.0.1:3001/#{File.readdirSync("#{__dirname}/fixtures/127.0.0.1:3001")[0]}"
- fixture = File.readFileSync(fixture_path, "utf8")
set_cookie_count = 0
+ files = File.readdirSync(@fixturesDir)
+ fixture = File.readFileSync("#{@fixturesDir}/#{files[0]}", "utf8")
for line in fixture.split("\n")
set_cookie_count++ if /set-cookie: c\d=v\d/.test(line)
assert.equal set_cookie_count, 2
describe "replaying multiple headers", ->
- headers = null
-
before (done)->
- Request.get "http://127.0.0.1:3001/set-cookie", (err, resp)->
- headers = resp.headers
+ Request.get "http://127.0.0.1:#{HTTP_PORT}/set-cookie", (err, resp)=>
+ @headers = resp.headers
done()
it "should have both set-cookie headers", ->
- assert.equal headers["set-cookie"][0], "c1=v1"
- assert.equal headers["set-cookie"][1], "c2=v2"
+ assert.equal @headers["set-cookie"][0], "c1=v1"
+ assert.equal @headers["set-cookie"][1], "c2=v2"
after ->
- File.unlinkSync(fixture_path)
+ for file in File.readdirSync(@fixturesDir)
+ File.unlinkSync("#{@fixturesDir}/#{file}")
+ File.rmdir(@fixturesDir)
- # Send responses to non-existent server on port 3002. No matching fixture for that path, expect a 404.
+ # Send responses to non-existent server on inactive port. No matching fixture for that path, expect a 404.
describe "undefined path", ->
- error = null
-
before ->
Replay.mode = "replay"
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, path: "/weather?c=14003", done)
- request.on "response", done
- request.on "error", (_)->
- error = _
- done()
+ HTTP.get(hostname: "example.com", port: INACTIVE_PORT, path: "/weather?c=14003", done)
+ .on "error", (@error)=>
+ done()
it "should callback with error", ->
- assert error instanceof Error
- assert.equal error.code, "ECONNREFUSED"
+ assert @error instanceof Error
+ assert.equal @error.code, "ECONNREFUSED"
- # Send responses to non-existent server on port 3002. No matching fixture for that host, expect refused connection.
+ # Send responses to non-existent server on inactive port. No matching fixture for that host, expect refused connection.
describe "undefined host", ->
- error = null
-
before ->
Replay.mode = "default"
before (done)->
- request = HTTP.get(hostname: "no-such", port: 3002)
- request.on "response", done
- request.on "error", (_)->
- error = _
- done()
+ HTTP.get(hostname: "no-such", port: INACTIVE_PORT, done)
+ .on "error", (@error)=>
+ done()
it "should callback with error", ->
- assert error instanceof Error
- assert.equal error.code, "ECONNREFUSED"
+ assert @error instanceof Error
+ assert.equal @error.code, "ECONNREFUSED"
# Mapping specifies a header, make sure we only match requests that have that header value.
@@ -192,74 +164,64 @@ describe "Replay", ->
Replay.mode = "replay"
describe "matching", ->
- statusCode = null
-
before (done)->
- request = HTTP.request(hostname: "example.com", port: 3002, path: "/weather.json")
+ request = HTTP.request(hostname: "example.com", port: INACTIVE_PORT, path: "/weather.json")
request.setHeader "Accept", "application/json"
- request.on "response", (response)->
- statusCode = response.statusCode
+ request.on "response", (response)=>
+ @statusCode = response.statusCode
response.on "end", done
- request.on "error", done
+ request.on("error", done)
request.end()
it "should return status code", ->
- console.dir arguments
- assert.equal statusCode, 200
+ assert.equal @statusCode, 200
describe "no match", ->
- error = null
-
before (done)->
- request = HTTP.request(hostname: "example.com", port: 3002, path: "/weather.json")
+ request = HTTP.request(hostname: "example.com", port: INACTIVE_PORT, path: "/weather.json")
request.setHeader "Accept", "text/xml"
request.on "response", (response)->
response.on "end", done
- request.on "error", (_)->
- error = _
+ request.on "error", (@error)=>
done()
request.end()
it "should fail to connnect", ->
- assert error instanceof Error
+ assert @error instanceof Error
describe "method", ->
- statusCode = headers = null
-
before ->
Replay.mode = "replay"
describe "matching", ->
before (done)->
- request = HTTP.request(hostname: "example.com", port: 3002, method: "post", path: "/posts")
+ request = HTTP.request(hostname: "example.com", port: INACTIVE_PORT, method: "post", path: "/posts")
request.setHeader "Accept", "application/json"
- request.on "response", (response)->
- { statusCode, headers } = response
+ request.on "response", (response)=>
+ { @statusCode, @headers } = response
response.on "end", done
request.on "error", done
request.end()
it "should return status code", ->
- assert.equal statusCode, 201
+ assert.equal @statusCode, 201
it "should return headers", ->
- assert.equal headers.location, "/posts/1"
+ assert.equal @headers.location, "/posts/1"
describe "no match", ->
- error = null
before (done)->
- request = HTTP.request(hostname: "example.com", port: 3002, method: "put", path: "/posts")
+ request = HTTP.request(hostname: "example.com", port: INACTIVE_PORT, method: "put", path: "/posts")
request.setHeader "Accept", "text/xml"
request.on "response", (response)->
response.on "end", done
- request.on "error", (_)->
- error = _
+ request.on "error", (@error)=>
done()
request.end()
it "should fail to connnect", ->
- assert error instanceof Error
+ assert @error instanceof Error
describe "minimal response", ->
@@ -267,27 +229,24 @@ describe "Replay", ->
Replay.mode = "replay"
describe "listeners", ->
- response = null
-
before (done)->
- request = HTTP.get(hostname: "example.com", port: 3002, path: "/minimal")
- request.on "response", (_)->
- response = _
- response.body = null
- response.on "data", (chunk)->
+ request = HTTP.get(hostname: "example.com", port: INACTIVE_PORT, path: "/minimal")
+ request.on "response", (@response)=>
+ @response.body = null
+ @response.on "data", (chunk)=>
# This will fail, no response.body
- response.body += chunk
- response.on "end", done
+ @response.body += chunk
+ @response.on "end", done
request.on "error", done
it "should return HTTP version", ->
- assert.equal response.httpVersion, "1.1"
+ assert.equal @response.httpVersion, "1.1"
it "should return status code", ->
- assert.equal response.statusCode, 200
+ assert.equal @response.statusCode, 200
it "should return no response headers", ->
- assert.deepEqual response.headers, { }
+ assert.deepEqual @response.headers, { }
it "should return no response trailers", ->
- assert.deepEqual response.trailers, { }
+ assert.deepEqual @response.trailers, { }
it "should return no response body", ->
- assert !response.body
+ assert !@response.body
Please sign in to comment.
Something went wrong with that request. Please try again.