Skip to navigation

Write tests for gRPC requests using JavaScript assertions in Postman

View as Markdown

You can write tests for your gRPC requests using scripts. Depending on your test logic and how you want to get the results, there are various ways to structure the test assertions in a test script. This page provides script examples for common gRPC testing scenarios in Postman.

The examples on this page use gRPC-specific properties of the pm.response object, such as pm.response.metadata, pm.response.trailers, and pm.response.messages. These properties are available only in scripts for gRPC requests.

For assertions that work with any request type, such as checking value types, array contents, or object properties, see Postman test script examples.

Test the status code

gRPC has its own set of status codes, where 0 indicates a successful response. You can use the statusCode property available over pm.response to test the status code of the response.

pm.test('Status code is 0', () => {
pm.response.to.have.statusCode(0);
});

You can also assert the same using the pm.expect method.

pm.test('Status code is 0', () => {
pm.expect(pm.response.statusCode).to.equal(0);
});

You can use the pm.response.to.be.ok as shorthand to test if the gRPC status code is 0.

Test the response time

For a request with a unary method, you can assert the response time:

pm.test('Response time is below 200ms', () => {
pm.response.to.have.responseTime.below(200);
// Using pm.expect
pm.expect(pm.response.responseTime).to.be.below(200);
});

For requests with streaming methods, pm.response.responseTime denotes the total duration for that request execution.

Test metadata

To check if response metadata is present:

pm.test('"content-type" is present in response metadata', () => {
pm.response.to.have.metadata('content-type');
// Using pm.expect
pm.expect(pm.response.metadata.has('content-type')).to.be.true;
});

You can also assert the value of the metadata:

pm.test('"content-type" response metadata is "application/grpc"', () => {
pm.response.to.have.metadata('content-type', 'application/grpc');
// Using pm.expect
pm.expect(pm.response.metadata.get('content-type')).to.equal('application/grpc');
});

Similar assertions can be written for request metadata using the pm.request object.

Test response trailers

To check if a response trailer is present:

pm.test('"grpc-status-details-bin" is present in response trailers', () => {
pm.response.to.have.trailer('grpc-status-details-bin');
// Using pm.expect
pm.expect(pm.response.trailers.has('grpc-status-details-bin')).to.be.true;
});

You can also assert the value of the trailer:

pm.test('"grpc-status-details-bin" response trailer is "dummy-value"', () => {
pm.response.to.have.trailer('grpc-status-details-bin', 'dummy-value');
// Using pm.expect
pm.expect(pm.response.trailers.get('grpc-status-details-bin')).to.equal('dummy-value');
});

Test responses

For requests with multiple response messages, such as those using a bidirectional streaming method, the tests in this section check all messages for the given assertion. For requests with a single response message, like those using unary or client streaming methods, the assertion is tested only on that message.

Also, when writing assertions using pm.response.messages.to.*, you will be asserting on an array of message content and not the complete pm.response message object.

You can test the assertions in this section on request messages as well using the pm.request object.

Test the existence of a message

To test the existence of a response message (strictly):

pm.test('Correct user details are received', () => {
pm.response.to.have.message({
userId: '123',
name: 'John Doe',
email: 'john@example.com',
phone: '+1-555-555-5555',
age: 30,
company: 'XYZ'
});
});

Test for a message with a specific property

You can assert that the given object’s properties are a subset of any messages received as a response:

pm.test('User details are updated successfully', () => {
pm.response.messages.to.include({
action: 'update-user-details',
status: 'success'
});
});

By default, pm.response.messages.to.include() has .deep applied to it.

Test for a common property across all messages

To check if a common property exists in all the received messages:

pm.test('All users have "company" in their profile', () => {
pm.response.messages.to.have.property('company');
});

You can also assert the value of a common property:

pm.test('All users are in same company', () => {
pm.response.messages.to.have.property('company', 'XYZ');
});

By default, pm.response.messages.to.have.property() has .deep and .nested applied to it.

Test messages against a JSON Schema

You can assert that the received messages match the given JSON Schema:

const schema = {
type: "object",
properties: {
username: {
type: "string",
pattern: "^[a-z0-9_-]{3,16}$"
}
}
};
pm.test('All response messages have correct username', () => {
pm.response.messages.to.have.jsonSchema(schema);
});
pm.test('Assert on a specific message', () => {
pm.expect(pm.response.messages.idx(10).data).to.have.jsonSchema(schema);
});

Work with a stream of messages

The examples below show how to work with a stream of messages and write assertions on them.

pm.test('Should receive keep-alive message roughly every 5 seconds', () => {
const keepAliveMessage = pm.response.messages.filter({
data: {
type: 'keep-alive'
}
});
for (let i = 1; i < keepAliveMessage.length; i++) {
const time1 = keepAliveMessage[i-1].timestamp;
const time2 = keepAliveMessage[i].timestamp;
pm.expect(time2-time1).to.be.within(4800, 5200);
}
});
pm.test('Every request message should have a corresponding response message', () => {
pm.request.messages.each((reqMsg) => {
pm.response.messages.to.include({ id: reqMsg.data.id });
});
});