> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# HCMockAddMock

> Adds a mock so that libHttpClient returns a mock response instead of making a network call.

Configures libHttpClient to return mock response instead of making a network call when HCHttpCallPerformAsync() is called.

## Syntax

```cpp theme={null}
HRESULT HCMockAddMock(  
         HCMockCallHandle call,  
         const char* method,  
         const char* url,  
         const uint8_t* requestBodyBytes,  
         uint32_t requestBodySize  
)  
```

### Parameters

*call*   \_In\_\
Type: HCMockCallHandle

This HC\_MOCK\_CALL\_HANDLE that represents the mock that has been configured accordingly using HCMockResponseSet\*()

*method*   \_In\_opt\_z\_\
Type: char\*

The UTF-8 encoded HTTP method. Pass it together with *url* to limit the mock to specific URLs. The method isn't used for matching.

*url*   \_In\_opt\_z\_\
Type: char\*

The UTF-8 encoded URL prefix to match. The URL condition is set only when both *method* and *url* aren't `nullptr`.

*requestBodyBytes*   \_In\_reads\_bytes\_opt\_(requestBodySize)\
Type: uint8\_t\*

If you want the mock to only apply to a specific URL & request string, pass in a method, URL and a string body.

*requestBodySize*   \_In\_\
Type: uint32\_t

The size of requestBodyBytes in bytes.

### Return value

Type: HRESULT

Result code for this API operation. Possible values are S\_OK, E\_INVALIDARG, E\_OUTOFMEMORY, E\_HC\_NOT\_INITIALISED, or E\_FAIL. Returns E\_HC\_NOT\_INITIALISED if libHttpClient isn't initialized; call [HCInitialize](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcinitialize.md) first.

## Remarks

To define a mock response, create a mock handle with [HCMockCallCreate](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockcallcreate.md), and then set the response with the HCMockResponseSet\* functions. Then add the mock by calling HCMockAddMock.

On a newly created mock, omit both *method* and *url* to match every call. To set a URL-prefix condition, pass both a non-null *method* and a nonempty *url*. Matching ignores the method. An empty stored URL matches every call and ignores any body condition. Null arguments don't clear criteria already stored on a reused mock. With a nonempty stored URL and a nonempty body filter, matching compares the buffered request-body bytes exactly. A non-null *requestBodyBytes* with a zero *requestBodySize* is invalid.

If a request supplies its body only through a custom body read function, don't rely on the body filter. Omit the body filter and inspect the request body in an [HCMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockmatchedcallback.md) instead.

You can add more than one mock that matches the same call. libHttpClient selects a mock for each request attempt, including automatic retries. If only one mock matches, libHttpClient uses that mock every time. If more than one mock matches, libHttpClient cycles through the matching mocks in the order that you added them, and starts again with the first after the last. libHttpClient tracks this position separately for each method and URL pair of the original call. A successful call to [HCMockRemoveMock](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockremovemock.md) or [HCMockClearMocks](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockclearmocks.md) resets the position for all calls.

Each successful call to HCMockAddMock retains a reference to the mock. Removing a registration with HCMockRemoveMock, or clearing all registrations with HCMockClearMocks, releases the corresponding retained references. You still need to close your own handle with [HCMockCallCloseHandle](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockcallclosehandle.md).

To change the response when a mock is matched, register an [HCMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockmatchedcallback.md) with [HCMockSetMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmocksetmockmatchedcallback.md).

## Requirements

**Header:** mock.h

**Library:** libHttpClient.lib

**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## See also

[mock](https://docs.xbox.com/reference/live/httpclient/mock/mock_members.md)\
[HCMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockmatchedcallback.md)\
[HCMockRemoveMock](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockremovemock.md)\
[HCMockClearMocks](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockclearmocks.md)


## Related topics

- [mock (contents)](https://docs.xbox.com/reference/live/httpclient/mock/mock_members.md)
- [HCMockClearMocks](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockclearmocks.md)
- [HCMockRemoveMock](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockremovemock.md)
- [HCMockSetMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmocksetmockmatchedcallback.md)
- [HCMockMatchedCallback](https://docs.xbox.com/reference/live/httpclient/mock/functions/hcmockmatchedcallback.md)
