Skip to content

Commit

Permalink
feat: implemented consumer DSL URL matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 26, 2020
1 parent e6a153f commit f27a444
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 12 deletions.
175 changes: 173 additions & 2 deletions src/v3/matchers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as mockery from "mockery"
const MockNative = {

let MockNative = {
generate_datetime_string: () => "",
generate_regex_string: () => ""
}
mockery.registerMock("../native", MockNative)

Expand Down Expand Up @@ -242,4 +243,174 @@ describe("V3 Matchers", () => {
})
})
})

describe("#string", () => {
it("returns a JSON representation of a like matcher", () => {
let result = matchers.string("true")
expect(result).to.deep.equal({
"pact:matcher:type": "type",
value: "true"
})
})
})

describe("#regex", () => {
it("returns a JSON representation of a regex matcher", () => {
let result = matchers.regex("\\d+", "1234")
expect(result).to.deep.equal({
"pact:matcher:type": "regex",
"regex": "\\d+",
value: "1234"
})
})

describe("when given a regular expression", () => {
it("returns a JSON representation of a regex matcher", () => {
let result = matchers.regex(/\d+/, "1234")
expect(result).to.deep.equal({
"pact:matcher:type": "regex",
"regex": "\\d+",
value: "1234"
})
})
})
})

describe("#equal", () => {
it("returns a JSON representation of an equality matcher", () => {
let result = matchers.equal("true")
expect(result).to.deep.equal({
"pact:matcher:type": "equality",
value: "true"
})
})
})

describe("#datetime", () => {
it("returns a JSON representation of a datetime matcher", () => {
let result = matchers.datetime("yyyy-MM-dd'T'HH:mm:ss.SSSX", "2016-02-11T09:46:56.023Z")
expect(result).to.deep.equal({
"pact:generator:type": "DateTime",
"pact:matcher:type": "timestamp",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSX",
value: "2016-02-11T09:46:56.023Z"
})
})

describe("when no example is given", () => {
it("generates a datetime from the current system time", () => {
MockNative.generate_datetime_string = () => "2016-02-11T09:46:56.023Z"
let result = matchers.datetime("yyyy-MM-dd'T'HH:mm:ss.SSSX")
expect(result).to.deep.equal({
"pact:generator:type": "DateTime",
"pact:matcher:type": "timestamp",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSX",
value: "2016-02-11T09:46:56.023Z"
})
})
})
})

describe("#time", () => {
it("returns a JSON representation of a time matcher", () => {
let result = matchers.time("HH:mm:ss", "09:46:56")
expect(result).to.deep.equal({
"pact:generator:type": "Time",
"pact:matcher:type": "time",
"format": "HH:mm:ss",
value: "09:46:56"
})
})

describe("when no example is given", () => {
it("generates a time from the current system time", () => {
MockNative.generate_datetime_string = () => "10:46:56.023"
let result = matchers.time("HH:mm:ss.SSS")
expect(result).to.deep.equal({
"pact:generator:type": "Time",
"pact:matcher:type": "time",
"format": "HH:mm:ss.SSS",
value: "10:46:56.023"
})
})
})
})

describe("#date", () => {
it("returns a JSON representation of a date matcher", () => {
let result = matchers.date("yyyy-MM-dd", "2016-02-11")
expect(result).to.deep.equal({
"pact:generator:type": "Date",
"pact:matcher:type": "date",
"format": "yyyy-MM-dd",
value: "2016-02-11"
})
})

describe("when no example is given", () => {
it("generates a date from the current system time", () => {
MockNative.generate_datetime_string = () => "2020-02-11"
let result = matchers.date("yyyy-MM-dd")
expect(result).to.deep.equal({
"pact:generator:type": "Date",
"pact:matcher:type": "date",
"format": "yyyy-MM-dd",
value: "2020-02-11"
})
})
})
})

describe("#includes", () => {
it("returns a JSON representation of an include matcher", () => {
let result = matchers.includes("true")
expect(result).to.deep.equal({
"pact:matcher:type": "include",
value: "true"
})
})
})

describe("#nullValue", () => {
it("returns a JSON representation of an null matcher", () => {
let result = matchers.nullValue()
expect(result).to.deep.equal({
"pact:matcher:type": "null"
})
})
})

describe("#url", () => {
it("returns a JSON representation of a regex matcher for the URL", () => {
let result = matchers.url('http://localhost:8080', ['users', '1234', 'posts', 'latest'])
expect(result).to.deep.equal({
"pact:matcher:type": "regex",
"regex": ".*\\/users\\/1234\\/posts\\/latest$",
"value": "http://localhost:8080/users/1234/posts/latest"
})
})

describe("when provided with a regex matcher", () => {
it("returns a JSON representation of a regex matcher for the URL", () => {
let result = matchers.url('http://localhost:8080', ['users', matchers.regex("\\d+", "1234"), 'posts', 'latest'])
expect(result).to.deep.equal({
"pact:matcher:type": "regex",
"regex": ".*\\/users\\/\\d+\\/posts\\/latest$",
"value": "http://localhost:8080/users/1234/posts/latest"
})
})
})

describe("when provided with a regular expression", () => {
it("returns a JSON representation of a regex matcher for the URL", () => {
MockNative.generate_regex_string = () => "12345678"
let result = matchers.url('http://localhost:8080', ['users', /\d+/, 'posts', 'latest'])
expect(result).to.deep.equal({
"pact:matcher:type": "regex",
"regex": ".*\\/users\\/\\d+\\/posts\\/latest$",
"value": "http://localhost:8080/users/12345678/posts/latest"
})
})
})
})
})
15 changes: 5 additions & 10 deletions src/v3/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ export function datetime(format: string, example?: string) {
"pact:generator:type": "DateTime",
"pact:matcher:type": "timestamp",
format,
timestamp: format, // This is needed due to a defect in upstream matching lib. Should be fixed in 0.6.2
value: example || PactNative.generate_datetime_string(format),
}
}
Expand All @@ -283,7 +282,6 @@ export function time(format: string, example?: string) {
"pact:generator:type": "Time",
"pact:matcher:type": "time",
format,
time: format, // This is needed due to a defect in upstream matching lib. Should be fixed in 0.6.2
value: example || PactNative.generate_datetime_string(format),
}
}
Expand All @@ -298,7 +296,6 @@ export function date(format: any, example?: string) {
format,
"pact:generator:type": "Date",
"pact:matcher:type": "date",
date: format, // This is needed due to a defect in upstream matching lib. Should be fixed in 0.6.2
value: example || PactNative.generate_datetime_string(format),
}
}
Expand Down Expand Up @@ -330,25 +327,23 @@ export function nullValue() {
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url(basePath: string, pathFragments: Array<any>) {
let regex = ""
let example = ""
let regex = ".*"
let example = basePath
for (let p of pathFragments) {
if (p instanceof Object && p["pact:matcher:type"] == "regex") {
regex += "\\/" + p["regex"]
example += "/" + p["value"]
} else if (p instanceof RegExp) {
regex += "\\/" + p.source
example += "/" + PactNative.generate_regex_string(p.source)
} else if (p instanceof string) {
regex += "\\/" + p
example += "/" + p
} else {
throw new Error("Only regex matchers, regular expressions or strings can be use a path fragments")
regex += "\\/" + p.toString()
example += "/" + p.toString()
}
}
return {
"pact:matcher:type": "regex",
regex,
regex: regex + '$',
value: example
}
}

0 comments on commit f27a444

Please sign in to comment.