|
1 | 1 | import * as Mockttp from 'mockttp'; |
2 | 2 | import * as serializr from 'serializr'; |
3 | 3 | import { observable } from 'mobx'; |
| 4 | +import * as HarFormat from 'har-format'; |
4 | 5 |
|
5 | 6 | import { HttpExchange, RawHeaders, HttpExchangeView } from "../../types"; |
6 | 7 | import { ObservablePromise } from '../../util/observable'; |
7 | | -import { h2HeadersToH1 } from '../http/headers'; |
8 | 8 |
|
9 | | -import { EditableContentType, getEditableContentTypeFromViewable } from "../events/content-types"; |
| 9 | +import { EditableContentType, getEditableContentType, getEditableContentTypeFromViewable } from "../events/content-types"; |
10 | 10 | import { EditableBody } from '../http/editable-body'; |
11 | 11 | import { |
12 | 12 | syncBodyToContentLength, |
13 | 13 | syncFormattingToContentType, |
14 | 14 | syncUrlToHeaders |
15 | 15 | } from '../http/editable-request-parts'; |
| 16 | +import { getHeaderValue, h2HeadersToH1 } from '../http/headers'; |
| 17 | +import { parseHarRequest } from '../http/har'; |
16 | 18 |
|
17 | 19 | // This is our model of a Request for sending. Smilar to the API model, |
18 | 20 | // but not identical, as we add extra UI metadata etc. |
@@ -132,6 +134,26 @@ export async function buildRequestInputFromExchange(exchange: HttpExchangeView): |
132 | 134 | }); |
133 | 135 | } |
134 | 136 |
|
| 137 | +export function buildRequestInputFromHarRequest(requestData: HarFormat.Request): RequestInput { |
| 138 | + const harRequest = parseHarRequest('', requestData, {} as any); |
| 139 | + |
| 140 | + let headers = harRequest.rawHeaders; |
| 141 | + if (parseInt(harRequest.httpVersion.split('.')[0], 10) >= 2) { |
| 142 | + headers = h2HeadersToH1(headers, harRequest.method); |
| 143 | + } |
| 144 | + |
| 145 | + return new RequestInput({ |
| 146 | + method: harRequest.method, |
| 147 | + url: harRequest.url, |
| 148 | + headers: headers, |
| 149 | + requestContentType: getEditableContentType( |
| 150 | + getHeaderValue(harRequest.headers, 'content-type') |
| 151 | + ?? 'application/octet-stream' |
| 152 | + ) ?? 'text', |
| 153 | + rawBody: harRequest.body.decoded |
| 154 | + }); |
| 155 | +} |
| 156 | + |
135 | 157 | // These are the types that the sever client API expects. They are _not_ the same as |
136 | 158 | // the Input type above, which is more flexible and includes various UI concerns that |
137 | 159 | // we don't need to share with the server to actually send the request. |
|
0 commit comments