Skip to content

Commit

Permalink
Merge pull request #1552 from microsoft/chore/add_3xx_tests
Browse files Browse the repository at this point in the history
fix: handle 3XX responses
  • Loading branch information
baywet authored Jan 16, 2025
2 parents d806e90 + 856de64 commit 5145d6a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/http/fetch/src/fetchRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ export class FetchRequestAdapter implements RequestAdapter {
};
public static readonly errorMappingFoundAttributeName = "com.microsoft.kiota.error.mapping_found";
public static readonly errorBodyFoundAttributeName = "com.microsoft.kiota.error.body_found";
private static readonly locationHeaderName = "Location";
private readonly throwIfFailedResponse = (response: Response, errorMappings: ErrorMappings | undefined, spanForAttributes: Span): Promise<void> => {
return trace.getTracer(this.observabilityOptions.getTracerInstrumentationName()).startActiveSpan("throwIfFailedResponse", async (span) => {
try {
if (response.ok) return;
if (response.ok || (response.status >= 300 && response.status < 400 && !response.headers.has(FetchRequestAdapter.locationHeaderName))) return;

spanForAttributes.setStatus({
code: SpanStatusCode.ERROR,
Expand Down
45 changes: 45 additions & 0 deletions packages/http/fetch/test/common/fetchRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,49 @@ describe("FetchRequestAdapter.ts", () => {
}
});
});
describe("returns null on 3XX responses without location header", () => {
it("should not throw API error when 3XX response with no Location header", async () => {
const mockHttpClient = new HttpClient();
mockHttpClient.executeFetch = async (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>) => {
const response = new Response(null, {
status: 304,
} as ResponseInit);
response.headers.set("Content-Type", "application/json");
return Promise.resolve(response);
};
const mockFactory = new MockParseNodeFactory(new MockParseNode({}));
const requestAdapter = new FetchRequestAdapter(new AnonymousAuthenticationProvider(), mockFactory, undefined, mockHttpClient);
const requestInformation = new RequestInformation();
requestInformation.URL = "https://www.example.com";
requestInformation.httpMethod = HttpMethod.GET;
try {
const result = await requestAdapter.send(requestInformation, createMockEntityFromDiscriminatorValue, undefined);
assert.isUndefined(result);
} catch (error) {
assert.fail("Should not throw an error");
}
});
it("should throw API error when 3XX response with a Location header", async () => {
const mockHttpClient = new HttpClient();
mockHttpClient.executeFetch = async (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>) => {
const response = new Response(null, {
status: 304,
} as ResponseInit);
response.headers.set("Content-Type", "application/json");
response.headers.set("Location", "xxx");
return Promise.resolve(response);
};
const mockFactory = new MockParseNodeFactory(new MockParseNode({}));
const requestAdapter = new FetchRequestAdapter(new AnonymousAuthenticationProvider(), mockFactory, undefined, mockHttpClient);
const requestInformation = new RequestInformation();
requestInformation.URL = "https://www.example.com";
requestInformation.httpMethod = HttpMethod.GET;
try {
await requestAdapter.sendNoResponseContent(requestInformation, undefined);
assert.fail("Should have thrown an error");
} catch (error) {
assert.equal(error.responseStatusCode, 304);
}
});
});
});

0 comments on commit 5145d6a

Please sign in to comment.