Skip to content

Commit

Permalink
split up the matcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropohedron committed Apr 5, 2015
1 parent 6201303 commit d2e62b8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 84 deletions.
11 changes: 11 additions & 0 deletions test/mock-channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function MockChannel(host, path) {
this.URI = {
host: host,
path: path
};
}

exports.createMockChannel = function createMockChannel(host, path) {
return new MockChannel(host, path);
};

77 changes: 77 additions & 0 deletions test/test-matcher-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Matcher = require("./matcher").Matcher;
var mock = require("./mock-channel.js");

exports["test exact host match"] = function(assert) {
var matcher = new Matcher([ "www.example.com" ]);
var channel = mock.createMockChannel("www.example.com", "/");
assert.strictEqual(matcher.testRequest(channel), true,
"Exact host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Exact host match response");
}

exports["test partial host match"] = function(assert) {
var channel = mock.createMockChannel("www.example.com", "/");
var matcher = new Matcher([ "www." ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Prefix host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Prefix host match response");
var matcher = new Matcher([ ".example.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Suffix host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Suffix host match response");
var matcher = new Matcher([ ".example." ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Middle host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Middle host match response");
var matcher = new Matcher([ "www.exam*.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Glob host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Glob host match response");
var matcher = new Matcher([ "www.exa??le.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Wildcard host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Wildcard host match response");
}

exports["test host nonmatch"] = function(assert) {
var channel = mock.createMockChannel("example.com", "/");
var matcher = new Matcher([ "www." ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Prefix host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Prefix host nonmatch response");
var matcher = new Matcher([ "example.co" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Incomplete host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Incomplete host nonmatch response");
var matcher = new Matcher([ ".ample.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Suffix host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Suffix host nonmatch response");
var matcher = new Matcher([ ".exam." ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Middle host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Middle host nonmatch response");
var matcher = new Matcher([ "*.example.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Glob host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Glob host nonmatch response");
var matcher = new Matcher([ "exa?mple.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Wildcard nonhost match request");
assert.strictEqual(matcher.testResponse(channel), false,
"Wildcard nonhost match response");
}

require("sdk/test").run(exports);

86 changes: 2 additions & 84 deletions test/test-matcher.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
var Matcher = require("./matcher").Matcher;

function MockChannel(host, path) {
this.URI = {
host: host,
path: path
};
}

function createMockChannel(host, path) {
return new MockChannel(host, path);
}

exports["test create matcher"] = function(assert) {
exports["test create matcher types"] = function(assert) {
var patterns = [
"TAG:^Content-Type: image/",
"NO-RESPONSE-TAG:^User-Agent: .*Internet",
Expand Down Expand Up @@ -44,76 +33,5 @@ exports["test create match invalid pattern"] = function(assert) {
}, /invalid pattern/i, "Invalid pattern passed to Matcher constructor");
};

exports["test exact host match"] = function(assert) {
var matcher = new Matcher([ "www.example.com" ]);
var channel = createMockChannel("www.example.com", "/");
assert.strictEqual(matcher.testRequest(channel), true,
"Exact host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Exact host match response");
}

exports["test partial host match"] = function(assert) {
var channel = createMockChannel("www.example.com", "/");
var matcher = new Matcher([ "www." ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Prefix host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Prefix host match response");
var matcher = new Matcher([ ".example.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Suffix host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Suffix host match response");
var matcher = new Matcher([ ".example." ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Middle host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Middle host match response");
var matcher = new Matcher([ "www.exam*.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Glob host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Glob host match response");
var matcher = new Matcher([ "www.exa??le.com" ]);
assert.strictEqual(matcher.testRequest(channel), true,
"Wildcard host match request");
assert.strictEqual(matcher.testResponse(channel), true,
"Wildcard host match response");
}

exports["test host nonmatch"] = function(assert) {
var channel = createMockChannel("example.com", "/");
var matcher = new Matcher([ "www." ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Prefix host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Prefix host nonmatch response");
var matcher = new Matcher([ "example.co" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Incomplete host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Incomplete host nonmatch response");
var matcher = new Matcher([ ".ample.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Suffix host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Suffix host nonmatch response");
var matcher = new Matcher([ ".exam." ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Middle host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Middle host nonmatch response");
var matcher = new Matcher([ "*.example.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Glob host nonmatch request");
assert.strictEqual(matcher.testResponse(channel), false,
"Glob host nonmatch response");
var matcher = new Matcher([ "exa?mple.com" ]);
assert.strictEqual(matcher.testRequest(channel), false,
"Wildcard nonhost match request");
assert.strictEqual(matcher.testResponse(channel), false,
"Wildcard nonhost match response");
}

require("sdk/test").run(exports);

0 comments on commit d2e62b8

Please sign in to comment.