package tezt-tezos

  1. Overview
  2. Docs

RPC description type and functions to call RPCs.

type verb =
  1. | GET
  2. | PUT
  3. | POST
  4. | PATCH
  5. | DELETE

HTTP request methods.

type data =
  1. | Data of Tezt_wrapper.JSON.u
  2. | File of string

Data type for RPCs.

val show_verb : verb -> string

Convert verb to string.

type 'result t = {
  1. verb : verb;
  2. path : string list;
  3. query_string : (string * string) list;
  4. data : data option;
  5. decode : Tezt_wrapper.JSON.t -> 'result;
}

RPC descriptions.

'result is the type of values returned by the RPC after decoding.

val make : ?data:data -> ?query_string:(string * string) list -> verb -> string list -> (Tezt_wrapper.JSON.t -> 'result) -> 'result t

Make an RPC description.

Usage: make ~get_host ~get_port verb path decode

  • If data is specified, it is provided as the request body.
  • If query_string is specified, it is added to the URL after the ? character. It is a list of key-value pairs. For instance, ~query_string: ["key1", "value1"; "key2", "value2"] denotes ?key1=value1&key2=value2.
  • path is the part of the URL that denotes the RPC's endpoint. For instance, ["chains"; "main"; "blocks"; "head"] denotes /chains/main/blocks/head.
  • decode is the function that will be used to decode the response body of the RPC. If you do not want to define it, use Fun.id (to return a JSON.t) or ignore (to ignore the response body).

Use one of the call functions below to actually call the RPC.

val make_uri : Endpoint.t -> 'result t -> Uri.t

make_uri endpoint rpc returns the URI of the RPC rpc at endpoint.

val decode_raw : ?origin:string -> 'result t -> string -> 'result

Parse and decode a response body using the decode function of an RPC description.

origin is used in error messages. Its default value is "RPC response".

val decode : 'result t -> Tezt_wrapper.JSON.t -> 'result

Decode a response body using the decode function of an RPC description.

type 'a response = {
  1. body : 'a;
    (*

    Response body.

    *)
  2. code : int;
    (*

    Status code (e.g. 200 for OK, 404 for Not Found).

    *)
}

RPC responses.

val check_string_response : ?body_rex:string -> code:int -> string response -> unit

check_string_response ?body_rex ~code response verifies that the given response's body and HTTP status match the expected ones using the facilities provided by module Check.

The function checks exact equality for the HTTP status and for regular expression matching for the body (if provided).

type rpc_hooks = {
  1. on_request : verb -> uri:string -> data option -> unit;
  2. on_response : Cohttp.Code.status_code -> body:string -> unit;
}

Callbacks that can be specified when calling RPCs.

on_request is called before each RPC call, and on_response is called after each successful RPC call (errors like 404 Not Found are considered successful RPC calls).

module type CALLERS = sig ... end
include CALLERS with type uri_provider := Endpoint.t
val call : ?rpc_hooks:rpc_hooks -> ?log_request:bool -> ?log_response_status:bool -> ?log_response_body:bool -> Endpoint.t -> 'result t -> 'result Lwt.t

Call an RPC.

The response body is parsed as JSON, then decoded using the decode function of the RPC description.

Calls Test.fail if the status code is not a success code. To handle error cases, use call_raw or call_json instead. They return the status code and you can use decode to parse the response body in case of success.

Parameter hooks allows to attach some hooks to the RPC.

If log_request is true, log the HTTP method and URI before calling the RPC. Default is true.

If log_response_status is true, log the HTTP status code of the response after the RPC call. Default is true.

If log_response_body is true, log the response body after the RPC call.

For call and call_json, if the response is valid JSON, it is pretty-printed with indentation. Default is true.

val call_raw : ?rpc_hooks:rpc_hooks -> ?log_request:bool -> ?log_response_status:bool -> ?log_response_body:bool -> Endpoint.t -> 'result t -> string response Lwt.t

Call an RPC, but do not parse its response body.

Does not fail if the status code is not a success code.

val call_json : ?rpc_hooks:rpc_hooks -> ?log_request:bool -> ?log_response_status:bool -> ?log_response_body:bool -> Endpoint.t -> 'result t -> Tezt_wrapper.JSON.t response Lwt.t

Call an RPC, but do not decode its response body, only parse as JSON.

Does not fail if the status code is not a success code, except if the response body is not valid JSON.

OCaml

Innovation. Community. Security.