Skip to content

Generate ssdk error tests #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

JordonPhillips
Copy link
Contributor

@JordonPhillips JordonPhillips commented Mar 23, 2021

This updates the http protocol test generator to include error tests. A generated test looks like:

it("RestJsonInvalidGreetingError:ServerErrorResponse", async () => {
  class TestService implements Partial<RestJsonService> {
    GreetingWithErrors(input: any, request: HttpRequest): GreetingWithErrorsServerOutput {
      const response = {
        Message: "Hi",
      } as any;
      const error: InvalidGreeting = {
        ...response,
        name: "InvalidGreeting",
        $fault: "client",
        $metadata: {},
      };
      throw error;
    }
  }
  const service: any = new TestService();
  const testMux = new httpbinding.HttpBindingMux<"RestJson", keyof RestJsonService>([
    new httpbinding.UriSpec<"RestJson", "GreetingWithErrors">("POST", [], [], {
      service: "RestJson",
      operation: "GreetingWithErrors",
    }),
  ]);
  class TestSerializer extends GreetingWithErrorsSerializer {
    deserialize = (output: any, context: any): Promise<any> => {
      return Promise.resolve({});
    };
  }
  const request = new HttpRequest({ method: "POST", hostname: "example.com" });
  const serFn: (
    op: RestJsonServiceOperations
  ) => __OperationSerializer<RestJsonService, RestJsonServiceOperations, __SmithyException> = (op) => {
    return new TestSerializer();
  };
  const handler = new RestJsonServiceHandler(service, testMux, serFn);
  let r = await handler.handle(request);

  expect(r.statusCode).toBe(400);

  expect(r.headers["content-type"]).toBeDefined();
  expect(r.headers["content-type"]).toBe("application/json");
  expect(r.headers["x-amzn-errortype"]).toBeDefined();
  expect(r.headers["x-amzn-errortype"]).toBe("InvalidGreeting");

  expect(r.body).toBeDefined();
  const utf8Encoder = __utf8Encoder;
  const bodyString = `{
                \"Message\": \"Hi\"
            }`;
  const unequalParts: any = compareEquivalentJsonBodies(bodyString, r.body.toString());
  expect(unequalParts).toBeUndefined();
});

Additionally, I tweaked the serializeError for operations with no modeled errors to immediately throw. Before it was creating an empty switch statement which would cause TypeScript to fail to compile since the errors union is never.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

This updates the error handler generation to just throw if there are
no modeled errors. This avoids compile time errors when trying to
access the `name` attribute.
@JordonPhillips JordonPhillips requested a review from kstich March 24, 2021 17:03
@JordonPhillips JordonPhillips merged commit 2e2664f into smithy-lang:ssdk Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants