Skip to content

Commit

Permalink
allow setting different expected msg for failing (300 <=) requests [#22]
Browse files Browse the repository at this point in the history
  • Loading branch information
Inchan Hwang committed Jul 27, 2020
1 parent 1070bba commit 0dde147
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/core/http_client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function translateResponse(
const responseHeaders = unconvertHeaders(response.headers);
const saidContentType = responseHeaders.find(([name]) => name === 'content-type')?.[1];

const { expectedProtobufMsg } = request;
const { expectedProtobufMsg, expectedProtobufMsgOnError } = request;

let responseBodyType: ResponseBodyType = 'unknown';
let responseBodyValue: ResponseBodyValue = undefined;
Expand All @@ -50,7 +50,12 @@ async function translateResponse(
responseBodyType = 'html';
responseBodyValue = toStr(buf);
} else if (expectedProtobufMsg) {
const res = await deserializeProtobuf(buf, expectedProtobufMsg, protoCtx);
let msgToUse = expectedProtobufMsg;
if (!response.ok && expectedProtobufMsgOnError) {
msgToUse = expectedProtobufMsgOnError;
}

const res = await deserializeProtobuf(buf, msgToUse, protoCtx);
switch (res.tag) {
case 'invalid':
if (res.value) {
Expand Down
1 change: 1 addition & 0 deletions src/core/http_client/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface RequestDescriptor {
readonly headers: ReadonlyArray<[string, string]>;
readonly body: Uint8Array | undefined;
readonly expectedProtobufMsg: string | undefined;
readonly expectedProtobufMsgOnError: string | undefined;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import React from 'react';
import { Select } from 'antd';
import { useDispatch } from 'react-redux';
import { selectResponseMessageName } from './ExpectedBodyInputActions';
import { selectResponseMessageName, selectResponseMessageOnErrorName } from './ExpectedBodyInputActions';
import { MESSAGE_NAME_WIDTH } from '../BodyInput/BodyInput';

type Props = {
messageNames: ReadonlyArray<string>;
expectedProtobufMsg: string | undefined;
expectedProtobufMsgOnError: string | undefined;
};

const ExpectedBodyInput: React.FunctionComponent<Props> = ({ messageNames, expectedProtobufMsg }) => {
const LABEL_STYLE = { display: 'inline-block', width: 100 };

const ExpectedBodyInput: React.FunctionComponent<Props> = ({
messageNames,
expectedProtobufMsg,
expectedProtobufMsgOnError,
}) => {
const dispatch = useDispatch();

function onSelectResponseMsg(msgName: string): void {
dispatch(selectResponseMessageName(msgName));
}

function onSelectResponseMsgOnError(msgName: string): void {
dispatch(selectResponseMessageOnErrorName(msgName));
}

return (
<div>
<span style={{ marginRight: 4 }}>Expected protobuf message: </span>
<span style={LABEL_STYLE}>On [200, 300):</span>
<Select
value={expectedProtobufMsg}
onChange={onSelectResponseMsg}
Expand All @@ -36,6 +47,27 @@ const ExpectedBodyInput: React.FunctionComponent<Props> = ({ messageNames, expec
</Select.Option>
))}
</Select>

<div style={{ height: 8 }} />

<span style={LABEL_STYLE}>On [300, ∞):</span>
<Select
value={expectedProtobufMsgOnError}
onChange={onSelectResponseMsgOnError}
size="small"
style={{ width: MESSAGE_NAME_WIDTH }}
allowClear
showSearch
filterOption={(input, option): boolean => {
return option && option.value.toString().includes(input.toString());
}}
>
{messageNames.map((messageName, idx) => (
<Select.Option key={idx} value={messageName}>
{messageName}
</Select.Option>
))}
</Select>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ type SelectResponseMessageName = {
name: string;
};

type SelectResponseMessageOnErrorName = {
type: 'SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME';
name: string;
};

const SELECT_RESPONSE_MESSAGE_NAME = 'SELECT_RESPONSE_MESSAGE_NAME';
const SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME = 'SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME';

export const ExpectedBodyInputActionTypes = [SELECT_RESPONSE_MESSAGE_NAME];
export type ExpectedBodyInputActions = SelectResponseMessageName;
export const ExpectedBodyInputActionTypes = [SELECT_RESPONSE_MESSAGE_NAME, SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME];
export type ExpectedBodyInputActions = SelectResponseMessageName | SelectResponseMessageOnErrorName;

export function selectResponseMessageName(name: string): SelectResponseMessageName {
return {
type: SELECT_RESPONSE_MESSAGE_NAME,
name,
};
}

export function selectResponseMessageOnErrorName(name: string): SelectResponseMessageOnErrorName {
return {
type: SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME,
name,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export default function ExpectedBodyInputReducer(s: AppState, action: AnyAction)
const flow = getByKey(collection.flows, draft.currentFlow);
if (!flow) return s;
flow.requestBuilder.expectedProtobufMsg = a.name;
if (!flow.requestBuilder.expectedProtobufMsgOnError) {
flow.requestBuilder.expectedProtobufMsgOnError = a.name;
}
});
case 'SELECT_RESPONSE_MESSAGE_ON_ERROR_NAME':
return produce(s, draft => {
const collection = getByKey(draft.collections, draft.currentCollection);
if (!collection) return s;
const flow = getByKey(collection.flows, draft.currentFlow);
if (!flow) return s;
flow.requestBuilder.expectedProtobufMsgOnError = a.name;
});
default:
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Props = {
};

const RequestBuilderView: React.FunctionComponent<Props> = ({ requestBuilder, protoCtx, messageNames, onSend }) => {
const { method, url, headers, bodyType, bodies, expectedProtobufMsg } = requestBuilder;
const { method, url, headers, bodyType, bodies, expectedProtobufMsg, expectedProtobufMsgOnError } = requestBuilder;

return (
<BuilderWrapper>
Expand All @@ -53,7 +53,11 @@ const RequestBuilderView: React.FunctionComponent<Props> = ({ requestBuilder, pr
<BodyInput bodyType={bodyType} bodies={bodies} protoCtx={protoCtx} messageNames={messageNames} />
</PaddedTabPane>
<PaddedTabPane tab="Expected Message" key="expectedMessage">
<ExpectedBodyInput messageNames={messageNames} expectedProtobufMsg={expectedProtobufMsg} />
<ExpectedBodyInput
messageNames={messageNames}
expectedProtobufMsg={expectedProtobufMsg}
expectedProtobufMsgOnError={expectedProtobufMsgOnError}
/>
</PaddedTabPane>
</Tabs>
</BuilderWrapper>
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/models/request_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export interface RequestBuilder {
readonly headers: ReadonlyArray<[string, string]>;
readonly bodyType: BodyType;
readonly bodies: RequestBody;
readonly expectedProtobufMsg: string | undefined;
readonly expectedProtobufMsg?: string;
readonly expectedProtobufMsgOnError?: string;
}

export interface RequestBody {
Expand All @@ -27,7 +28,7 @@ export async function toRequestDescriptor(
env: Env,
ctx: ProtoCtx,
): Promise<RequestDescriptor> {
const { url, method, headers, bodyType, bodies, expectedProtobufMsg } = builder;
const { url, method, headers, bodyType, bodies, expectedProtobufMsg, expectedProtobufMsgOnError } = builder;
const varMap = toVarMap(env);

let body;
Expand All @@ -44,5 +45,6 @@ export async function toRequestDescriptor(
headers: headers.map(([k, v]) => [k, applyEnvs(v, varMap)]),
body,
expectedProtobufMsg,
expectedProtobufMsgOnError,
};
}

0 comments on commit 0dde147

Please sign in to comment.