|
1 | 1 | import Adapter from '@pollyjs/adapter';
|
2 | 2 |
|
3 | 3 | const LISTENERS = Symbol();
|
| 4 | +const POLLY_REQUEST = Symbol(); |
4 | 5 | const PASSTHROUGH_PROMISE = Symbol();
|
5 | 6 | const PASSTHROUGH_PROMISES = Symbol();
|
6 | 7 | const PASSTHROUGH_REQ_ID_HEADER = 'x-pollyjs-passthrough-request-id';
|
@@ -73,24 +74,68 @@ export default class PuppeteerAdapter extends Adapter {
|
73 | 74 | const request = response.request();
|
74 | 75 |
|
75 | 76 | // Resolve the passthrough promise with the response if it exists
|
76 |
| - request[PASSTHROUGH_PROMISE] && |
| 77 | + if (request[PASSTHROUGH_PROMISE]) { |
77 | 78 | request[PASSTHROUGH_PROMISE].resolve(response);
|
78 |
| - |
79 |
| - delete request[PASSTHROUGH_PROMISE]; |
| 79 | + delete request[PASSTHROUGH_PROMISE]; |
| 80 | + } |
| 81 | + }, |
| 82 | + requestfinished: request => { |
| 83 | + // Resolve the deferred pollyRequest promise if it exists |
| 84 | + if (request[POLLY_REQUEST]) { |
| 85 | + request[POLLY_REQUEST].promise.resolve(request.response()); |
| 86 | + delete request[POLLY_REQUEST]; |
| 87 | + } |
80 | 88 | },
|
81 | 89 | requestfailed: request => {
|
82 | 90 | // Reject the passthrough promise with the error object if it exists
|
83 |
| - request[PASSTHROUGH_PROMISE] && |
| 91 | + if (request[PASSTHROUGH_PROMISE]) { |
84 | 92 | request[PASSTHROUGH_PROMISE].reject(request.failure());
|
| 93 | + delete request[PASSTHROUGH_PROMISE]; |
| 94 | + } |
85 | 95 |
|
86 |
| - delete request[PASSTHROUGH_PROMISE]; |
| 96 | + // Reject the deferred pollyRequest promise with the error object if it exists |
| 97 | + if (request[POLLY_REQUEST]) { |
| 98 | + request[POLLY_REQUEST].promise.reject(request.failure()); |
| 99 | + delete request[POLLY_REQUEST]; |
| 100 | + } |
87 | 101 | },
|
88 | 102 | close: () => this[LISTENERS].delete(page)
|
89 | 103 | });
|
90 | 104 |
|
91 | 105 | this._callListenersWith('prependListener', page);
|
92 | 106 | }
|
93 | 107 |
|
| 108 | + onRequest(pollyRequest) { |
| 109 | + const [request] = pollyRequest.requestArguments; |
| 110 | + |
| 111 | + /* |
| 112 | + Create an access point to the `pollyRequest` so it can be accessed from |
| 113 | + the emitted page events |
| 114 | + */ |
| 115 | + request[POLLY_REQUEST] = pollyRequest; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Override the onRequestFinished logic as it doesn't apply to this adapter. |
| 120 | + * Instead, that logic is re-implemented via the `requestfinished` page |
| 121 | + * event. |
| 122 | + * |
| 123 | + * @override |
| 124 | + */ |
| 125 | + onRequestFinished() {} |
| 126 | + |
| 127 | + /** |
| 128 | + * Abort the request on failure. The parent `onRequestFailed` has been |
| 129 | + * re-implemented via the `requestfailed` page event. |
| 130 | + * |
| 131 | + * @override |
| 132 | + */ |
| 133 | + async onRequestFailed(pollyRequest) { |
| 134 | + const [request] = pollyRequest.requestArguments; |
| 135 | + |
| 136 | + await request.abort(); |
| 137 | + } |
| 138 | + |
94 | 139 | async onRecord(pollyRequest) {
|
95 | 140 | await this.passthroughRequest(pollyRequest);
|
96 | 141 | await this.persister.recordRequest(pollyRequest);
|
|
0 commit comments