> This page is for Postman Docs, version Latest (v12) (default).
> For other versions, use one of these documentation indexes:
> - Latest (v12) (default): https://learning.postman.com/latest-v-12/llms.txt
> - v11: https://learning.postman.com/v11/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://learning.postman.com/llms.txt.

# Write tests for gRPC requests using JavaScript assertions in Postman

You can write tests for your gRPC requests using [scripts](/docs/use/send-requests/protocols/grpc/scripting-in-grpc-request/). 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](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/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.

> **Info**
>
> For assertions that work with any request type, such as checking value types, array contents, or object properties, see [Postman test script examples](/docs/tests-and-scripts/write-scripts/test-examples/).

## Test the status code \[#testing-status-code]

gRPC has its own set of [status codes](https://grpc.github.io/grpc/core/md_doc_statuscodes.html), where `0` indicates a successful response. You can use the `statusCode` property available over [pm.response](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/pm-response/) to test the status code of the response.

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

You can also assert the same using the [pm.expect](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/pm-test-expect/) method.

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

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

## Test the response time \[#testing-response-time]

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

```javascript
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 \[#testing-metadata]

To check if response metadata is present:

```javascript
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:

```javascript
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](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/pm-request/) object.

## Test response trailers \[#testing-response-trailers]

To check if a response trailer is present:

```javascript
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:

```javascript
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 \[#testing-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](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/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 \[#testing-existence-of-a-message]

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

```javascript
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 \[#testing-for-a-message-with-specific-property]

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

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

> **Info**
>
> By default, `pm.response.messages.to.include()` has `.deep` applied to it.

### Test for a common property across all messages \[#testing-for-a-common-property-across-all-messages]

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

```javascript
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:

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

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

### Test messages against a JSON Schema \[#testing-messages-against-a-json-schema]

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

```javascript
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 \[#working-with-a-stream-of-messages]

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

```javascript
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);
  }
});
```

```javascript
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 });
  });
});
```