Skip to content

Commit

Permalink
Modify parsing of response for Connect unary requests (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
smaye81 committed Jun 7, 2023
1 parent 6fdde58 commit 61a43ae
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ it like a web server would usually do.

| code generator | bundle size | minified | compressed |
|----------------|-------------------:|-----------------------:|---------------------:|
| connect | 112,660 b | 49,405 b | 13,203 b |
| connect | 112,742 b | 49,450 b | 13,229 b |
| grpc-web | 414,906 b | 301,127 b | 53,279 b |
111 changes: 111 additions & 0 deletions packages/connect-web-test/src/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { TestService } from "./gen/grpc/testing/test_connect.js";
import {
SimpleRequest,
SimpleResponse,
} from "./gen/grpc/testing/messages_pb.js";
import { createConnectTransport } from "@bufbuild/connect-web";

describe("custom fetch", function () {
describe("with Connect transport", () => {
it("should only call Response#json with the JSON format", async function () {
const response = new Response(
new SimpleResponse({ username: "donald" }).toJsonString(),
{
headers: {
"Content-Type": "application/json",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
baseUrl: "https://example.com",
fetch: () => Promise.resolve(response),
});
await transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
);
expect(response.json).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
});
it("should only call Response#arrayBuffer with the binary format on the happy path", async function () {
const response = new Response(
new SimpleResponse({ username: "donald" }).toBinary(),
{
headers: {
"Content-Type": "application/proto",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
fetch: () => Promise.resolve(response),
baseUrl: "https://example.com",
useBinaryFormat: true,
});
await transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
);
expect(response.json).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
});
it("should call Response#json with the binary format for an error response", async function () {
const response = new Response(
JSON.stringify({
code: "permission_denied",
message: "foobar",
}),
{
status: 403,
headers: {
"Content-Type": "application/json",
},
}
);
spyOn(response, "arrayBuffer").and.callThrough();
spyOn(response, "json").and.callThrough();
const transport = createConnectTransport({
fetch: () => Promise.resolve(response),
baseUrl: "https://example.com",
useBinaryFormat: true,
});
await expectAsync(
transport.unary(
TestService,
TestService.methods.unaryCall,
undefined,
undefined,
undefined,
new SimpleRequest()
)
).toBeRejectedWithError(/\[permission_denied] foobar/);
expect(response.json).toHaveBeenCalledTimes(1); // eslint-disable-line @typescript-eslint/unbound-method
expect(response.arrayBuffer).toHaveBeenCalledTimes(0); // eslint-disable-line @typescript-eslint/unbound-method
});
});
});
7 changes: 6 additions & 1 deletion packages/connect-web/src/connect-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ export function createConnectTransport(
service,
method,
header: demuxedHeader,
message: parse(new Uint8Array(await response.arrayBuffer())),
message: useBinaryFormat
? parse(new Uint8Array(await response.arrayBuffer()))
: method.O.fromJson(
(await response.json()) as JsonValue,
options.jsonOptions
),
trailer: demuxedTrailer,
};
},
Expand Down

0 comments on commit 61a43ae

Please sign in to comment.