|
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | import { ServiceClient } from "@azure/core-client"; |
5 | | -import { isPlaybackMode, Recorder } from "../src"; |
| 5 | +import { CustomMatcherOptions, isPlaybackMode, Recorder } from "../src"; |
6 | 6 | import { isLiveMode, TestMode } from "../src/utils/utils"; |
7 | 7 | import { getTestServerUrl, makeRequestAndVerifyResponse, setTestMode } from "./utils/utils"; |
8 | 8 |
|
@@ -102,6 +102,125 @@ import { getTestServerUrl, makeRequestAndVerifyResponse, setTestMode } from "./u |
102 | 102 | { val: "abc" } |
103 | 103 | ); |
104 | 104 | }); |
| 105 | + |
| 106 | + describe("CustomDefaultMatcher", () => { |
| 107 | + it("excludedHeaders - header value is different", async () => { |
| 108 | + const headerName = `X-Test-Dynamic-Header`; |
| 109 | + await recorder.start({ envSetupForPlayback: {} }); |
| 110 | + await recorder.setMatcher("CustomDefaultMatcher", { |
| 111 | + excludedHeaders: [headerName], |
| 112 | + }); |
| 113 | + |
| 114 | + const testHeader = { |
| 115 | + headerName, // dynamic header |
| 116 | + value: isPlaybackMode() ? "playback" : "record", |
| 117 | + }; |
| 118 | + |
| 119 | + await makeRequestAndVerifyResponse( |
| 120 | + client, |
| 121 | + { |
| 122 | + path: `/sample_response`, |
| 123 | + body: "body", |
| 124 | + method: "POST", |
| 125 | + headers: [{ headerName: "Content-Type", value: "text/plain" }, testHeader], |
| 126 | + }, |
| 127 | + { val: "abc" } |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it("excludedHeaders - header is non-existent", async () => { |
| 132 | + const headerName = `X-Test-Dynamic-Header`; |
| 133 | + await recorder.start({ envSetupForPlayback: {} }); |
| 134 | + await recorder.setMatcher("CustomDefaultMatcher", { |
| 135 | + excludedHeaders: [headerName], |
| 136 | + }); |
| 137 | + |
| 138 | + const testHeader = { |
| 139 | + headerName, // dynamic header |
| 140 | + value: "record", |
| 141 | + }; |
| 142 | + |
| 143 | + await makeRequestAndVerifyResponse( |
| 144 | + client, |
| 145 | + { |
| 146 | + path: `/sample_response`, |
| 147 | + body: "body", |
| 148 | + method: "POST", |
| 149 | + headers: [{ headerName: "Content-Type", value: "text/plain" }].concat( |
| 150 | + !isPlaybackMode() ? [testHeader] : [] |
| 151 | + ), |
| 152 | + }, |
| 153 | + { val: "abc" } |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it("ignoredHeaders", async () => { |
| 158 | + const headerName = `X-Test-Dynamic-Header`; |
| 159 | + await recorder.start({ envSetupForPlayback: {} }); |
| 160 | + await recorder.setMatcher("CustomDefaultMatcher", { |
| 161 | + ignoredHeaders: [headerName], |
| 162 | + } as CustomMatcherOptions); |
| 163 | + |
| 164 | + const testHeader = { |
| 165 | + headerName, // dynamic header |
| 166 | + value: isPlaybackMode() ? "playback" : "record", |
| 167 | + }; |
| 168 | + |
| 169 | + await makeRequestAndVerifyResponse( |
| 170 | + client, |
| 171 | + { |
| 172 | + path: `/sample_response`, |
| 173 | + body: "body", |
| 174 | + method: "POST", |
| 175 | + headers: [{ headerName: "Content-Type", value: "text/plain" }, testHeader], |
| 176 | + }, |
| 177 | + { val: "abc" } |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it("compareBodies", async () => { |
| 182 | + await recorder.start({ envSetupForPlayback: {} }); |
| 183 | + await recorder.setMatcher("CustomDefaultMatcher", { |
| 184 | + compareBodies: false, |
| 185 | + ignoredHeaders: ["Content-Length"], // adding this header since the body sizes are different |
| 186 | + } as CustomMatcherOptions); |
| 187 | + |
| 188 | + // The body shouldn't matter for the match; verify this by using a |
| 189 | + // different body in playback vs record mode. |
| 190 | + const body = isPlaybackMode() ? "playback" : "record"; |
| 191 | + |
| 192 | + await makeRequestAndVerifyResponse( |
| 193 | + client, |
| 194 | + { |
| 195 | + path: `/sample_response`, |
| 196 | + body, |
| 197 | + method: "POST", |
| 198 | + headers: [{ headerName: "Content-Type", value: "text/plain" }], |
| 199 | + }, |
| 200 | + { val: "abc" } |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it("ignoreQueryOrdering", async () => { |
| 205 | + await recorder.start({ envSetupForPlayback: {} }); |
| 206 | + await recorder.setMatcher("CustomDefaultMatcher", { |
| 207 | + ignoreQueryOrdering: true, |
| 208 | + }); |
| 209 | + |
| 210 | + await makeRequestAndVerifyResponse( |
| 211 | + client, |
| 212 | + { |
| 213 | + path: `/sample_response${ |
| 214 | + isPlaybackMode() ? "?first=abc&second=def" : "?second=def&first=abc" |
| 215 | + }`, |
| 216 | + body: undefined, |
| 217 | + method: "POST", |
| 218 | + headers: [{ headerName: "Content-Type", value: "text/plain" }], |
| 219 | + }, |
| 220 | + { val: "abc" } |
| 221 | + ); |
| 222 | + }); |
| 223 | + }); |
105 | 224 | }); |
106 | 225 |
|
107 | 226 | // Transforms |
|
0 commit comments