Allow overriding httpcli behavior by tests.

Change on 2014/12/17 by ctiller <ctiller@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=82379331
pull/1/merge
ctiller 10 years ago committed by Michael Lumish
parent 246ec3bd3a
commit 40260c4d5b
  1. 23
      src/core/httpcli/httpcli.c
  2. 15
      src/core/httpcli/httpcli.h

@ -62,6 +62,9 @@ typedef struct {
void *user_data;
} internal_request;
static grpc_httpcli_get_override g_get_override = NULL;
static grpc_httpcli_post_override g_post_override = NULL;
static void next_address(internal_request *req);
static void finish(internal_request *req, int success) {
@ -217,7 +220,12 @@ static void on_resolved(void *arg, grpc_resolved_addresses *addresses) {
void grpc_httpcli_get(const grpc_httpcli_request *request,
gpr_timespec deadline,
grpc_httpcli_response_cb on_response, void *user_data) {
internal_request *req = gpr_malloc(sizeof(internal_request));
internal_request *req;
if (g_get_override &&
g_get_override(request, deadline, on_response, user_data)) {
return;
}
req = gpr_malloc(sizeof(internal_request));
memset(req, 0, sizeof(*req));
req->request_text = grpc_httpcli_format_get_request(request);
grpc_httpcli_parser_init(&req->parser);
@ -237,7 +245,12 @@ void grpc_httpcli_post(const grpc_httpcli_request *request,
const char *body_bytes, size_t body_size,
gpr_timespec deadline,
grpc_httpcli_response_cb on_response, void *user_data) {
internal_request *req = gpr_malloc(sizeof(internal_request));
internal_request *req;
if (g_post_override && g_post_override(request, body_bytes, body_size,
deadline, on_response, user_data)) {
return;
}
req = gpr_malloc(sizeof(internal_request));
memset(req, 0, sizeof(*req));
req->request_text =
grpc_httpcli_format_post_request(request, body_bytes, body_size);
@ -253,3 +266,9 @@ void grpc_httpcli_post(const grpc_httpcli_request *request,
grpc_resolve_address(request->host, req->use_ssl ? "https" : "http",
on_resolved, req);
}
void grpc_httpcli_set_override(grpc_httpcli_get_override get,
grpc_httpcli_post_override post) {
g_get_override = get;
g_post_override = post;
}

@ -100,4 +100,19 @@ void grpc_httpcli_post(const grpc_httpcli_request *request,
gpr_timespec deadline,
grpc_httpcli_response_cb on_response, void *user_data);
/* override functions return 1 if they handled the request, 0 otherwise */
typedef int (*grpc_httpcli_get_override)(const grpc_httpcli_request *request,
gpr_timespec deadline,
grpc_httpcli_response_cb on_response,
void *user_data);
typedef int (*grpc_httpcli_post_override)(const grpc_httpcli_request *request,
const char *body_bytes,
size_t body_size,
gpr_timespec deadline,
grpc_httpcli_response_cb on_response,
void *user_data);
void grpc_httpcli_set_override(grpc_httpcli_get_override get,
grpc_httpcli_post_override post);
#endif /* __GRPC_INTERNAL_HTTPCLI_HTTPCLI_H__ */

Loading…
Cancel
Save