Skip to content

Commit

Permalink
support-accept-in-headers (#29206)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

@azure-rest/core-client 

### Issues associated with this PR
Azure/autorest.typescript#2210

### Describe the problem that is addressed by this PR
we can't have a way to pass accept header to the core now. although we
can do that in RLC as it's defined in
https://github.com/qiaozha/azure-sdk-for-js/blob/93cbd84e4ac13292a8d07af194f5888cf37aa361/sdk/core/core-client-rest/src/common.ts#L34,
but in modular, the operation options is extended from
https://github.com/qiaozha/azure-sdk-for-js/blob/93cbd84e4ac13292a8d07af194f5888cf37aa361/sdk/core/core-client-rest/src/common.ts#L121
we need to leverage the headers for accept for this to work in Modular.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
qiaozha committed Apr 10, 2024
1 parent 60d2100 commit d312f0c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sdk/core/core-client-rest/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- Support accept in headers.

### Breaking Changes

### Bugs Fixed
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function buildPipelineRequest(

const headers = createHttpHeaders({
...(options.headers ? options.headers : {}),
accept: options.accept ?? "application/json",
accept: options.accept ?? options.headers?.accept ?? "application/json",
...(hasContent &&
requestContentType && {
"content-type": requestContentType,
Expand Down
10 changes: 10 additions & 0 deletions sdk/core/core-client-rest/test/sendRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ describe("sendRequest", () => {
await sendRequest("POST", mockBaseUrl, mockPipeline, { accept: "testContent" });
});

it("should set custom accept via headers", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("accept"), "testContent");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, { headers: { accept: "testContent" } });
});

it("should set custom headers", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/ts-http-runtime/src/client/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function buildPipelineRequest(

const headers = createHttpHeaders({
...(options.headers ? options.headers : {}),
accept: options.accept ?? "application/json",
accept: options.accept ?? options.headers?.accept ?? "application/json",
...(hasContent &&
requestContentType && {
"content-type": requestContentType,
Expand Down
10 changes: 10 additions & 0 deletions sdk/core/ts-http-runtime/test/client/sendRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ describe("sendRequest", () => {
await sendRequest("POST", mockBaseUrl, mockPipeline, { accept: "testContent" });
});

it("should set custom accept via headers", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
assert.equal(request.headers.get("accept"), "testContent");
return { headers: createHttpHeaders() } as PipelineResponse;
};

await sendRequest("POST", mockBaseUrl, mockPipeline, { headers: { accept: "testContent" } });
});

it("should set custom headers", async () => {
const mockPipeline: Pipeline = createEmptyPipeline();
mockPipeline.sendRequest = async (_client, request) => {
Expand Down

0 comments on commit d312f0c

Please sign in to comment.