Skip to content

Commit

Permalink
feat: Update URL matching functions to support mock server URL genera…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Ronald Holshausen committed Jan 5, 2021
1 parent 3696d06 commit 2733af9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 64 deletions.
24 changes: 12 additions & 12 deletions examples/v3/provider-state-injected/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/v3/provider-state-injected/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@pact-foundation/pact": "^10.0.0-beta.21",
"@pact-foundation/pact": "^10.0.0-beta.22",
"jest": "^26.6.3"
},
"dependencies": {
Expand Down
34 changes: 10 additions & 24 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ neon-build = "0.6.0"

[dependencies]
neon = { version = "0.6.0", features = ["event-handler-api"] }
pact_mock_server = "0.7.12"
pact_mock_server_ffi = ">=0.0.12"
pact_matching = "0.8.6"
pact_mock_server = "0.7.13"
pact_mock_server_ffi = ">=0.0.13"
pact_matching = "0.8.7"
pact_verifier = "0.10.0"
env_logger = "0.8"
log = "0.4.6"
Expand Down
6 changes: 3 additions & 3 deletions src/v3/matchers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ describe("V3 Matchers", () => {

describe("#url", () => {
it("returns a JSON representation of a regex matcher for the URL", () => {
let result = MatchersV3.url("http://localhost:8080", [
let result = MatchersV3.url2("http://localhost:8080", [
"users",
"1234",
"posts",
Expand All @@ -438,7 +438,7 @@ describe("V3 Matchers", () => {

describe("when provided with a regex matcher", () => {
it("returns a JSON representation of a regex matcher for the URL", () => {
let result = MatchersV3.url("http://localhost:8080", [
let result = MatchersV3.url2("http://localhost:8080", [
"users",
MatchersV3.regex("\\d+", "1234"),
"posts",
Expand All @@ -455,7 +455,7 @@ describe("V3 Matchers", () => {
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 = MatchersV3.url("http://localhost:8080", [
let result = MatchersV3.url2("http://localhost:8080", [
"users",
/\d+/,
"posts",
Expand Down
44 changes: 31 additions & 13 deletions src/v3/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export namespace MatchersV3 {
/**
* Pact Matcher
*/
interface Matcher {
export interface Matcher {
"pact:matcher:type": string
"pact:generator:type"?: string
value?: any
Expand Down Expand Up @@ -353,30 +353,48 @@ export namespace MatchersV3 {
}
}

/**
* Matches a URL composed of a list of path fragments. The base URL from the mock server will be used.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url(pathFragments: Array<string | RegexMatcher| RegExp>): RegexMatcher {
return url2(null, pathFragments)
}

/**
* Matches a URL composed of a base path and a list of path fragments
* @param basePath Base path of the URL
* @param basePath Base path of the URL. If null, will use the base URL from the mock server.
* @param pathFragments list of path fragments, can be regular expressions
*/
export function url(basePath: string, pathFragments: Array<any>): RegexMatcher {
let regex = ".*"
let example = basePath
export function url2(basePath: string | null, pathFragments: Array<string | RegexMatcher | RegExp>): RegexMatcher {
let regex = ".*("
let example = basePath || "http://localhost:8080"
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) {
if (p instanceof RegExp) {
regex += "\\/" + p.source
example += "/" + PactNative.generate_regex_string(p.source)
} else if (p instanceof Object && p["pact:matcher:type"] == "regex") {
regex += "\\/" + p["regex"]
example += "/" + p["value"]
} else {
regex += "\\/" + p.toString()
example += "/" + p.toString()
}
}
return {
"pact:matcher:type": "regex",
regex: regex + "$",
value: example,

if (basePath == null) {
return {
"pact:matcher:type": "regex",
"pact:generator:type": "MockServerURL",
regex: regex + ")$",
value: example,
}
} else {
return {
"pact:matcher:type": "regex",
regex: regex + ")$",
value: example,
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/v3/pact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { omit, join, chain, toPairs, map, flatten } from "ramda"
import { omit, join, toPairs, map, flatten } from "ramda"
import { MatchersV3 } from "./matchers"

const pkg = require("../common/metadata")
const PactNative = require("../native/index.node")

Expand Down Expand Up @@ -35,20 +37,20 @@ export interface V3ProviderState {

export interface V3Request {
method?: string
path?: string
path?: string | MatchersV3.Matcher
query?: {
[param: string]: string
[param: string]: string | MatchersV3.Matcher
}
headers?: {
[header: string]: string
[header: string]: string | MatchersV3.Matcher
}
body?: any
}

export interface V3Response {
status: number
headers?: {
[header: string]: string
[header: string]: string | MatchersV3.Matcher
}
body?: any
}
Expand All @@ -59,17 +61,17 @@ export interface V3MockServer {
id: string
}

function displayQuery(query: { [k: string]: string[] }) {
function displayQuery(query: { [k: string]: string[] }): string {
let pairs = toPairs(query)
let mapped = flatten(map(([key, values]) => map((val) => `${key}=${val}`, values), pairs))
return join('&', mapped)
}

function displayHeaders(headers: any, indent: string) {
function displayHeaders(headers: any, indent: string): string {
return join('\n' + indent, map(([k, v]) => `${k}: ${v}`, toPairs(headers)))
}

function displayRequest(request: any, indent: string) {
function displayRequest(request: any, indent: string): string {
let output = `\n${indent}Method: ${request.method}\n${indent}Path: ${request.path}`

if (request.query) {
Expand Down

0 comments on commit 2733af9

Please sign in to comment.