1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
<!ENTITY % local.common.attrib "xmlns:xi CDATA #FIXED 'http://www.w3.org/2003/XInclude'">
<!ENTITY version SYSTEM "version.xml">
]>
<book id="index" xmlns:xi="http://www.w3.org/2003/XInclude">
<bookinfo>
<title>libuhttpmock Reference Manual</title>
<releaseinfo>for libuhttpmock &version;.<!-- The latest version of this documentation can be found online at
<ulink role="online-location" url="http://[SERVER]/libuhttpmock/index.html">http://[SERVER]/libuhttpmock/</ulink>.-->
</releaseinfo>
</bookinfo>
<part>
<title>Integrating libuhttpmock into new projects</title>
<chapter>
<title>Introduction</title>
<para>
libuhttpmock works by recording the Soup Session your application uses.
It traces the requests your application makes and the reply it receives
from the (online) server you query. After capturing an initial trace,
libuhttpmock will be able to reply to your application's requests
the same way the actual servers would and as such help you detect
unwanted changes in behaviour in your application.
</para>
<example>
<title>A sample implementation of a test with libuhttpmock</title>
<programlisting>
/*
* Copyright (C) 2020 Rasmus Thomsen <oss@cogitri.dev>
*/
#include <gio/gio.h>
#include <glib.h>
#include <libsoup/soup.h>
#include <uhttpmock/uhm.h>
UhmServer* mock_server = NULL;
static gboolean
accept_cert (SoupMessage *msg, GTlsCertificate *cert, GTlsCertificateFlags errors, gpointer user_data)
{
/*
* Allow usage of the self-signed certificate we generate in main ().
* Only to be done when testing!
*/
return uhm_server_get_enable_online (mock_server);
}
void
test_mock (void)
{
g_autoptr(GError) error = NULL;
g_autoptr(SoupLogger) soup_logger = NULL;
g_autoptr(SoupMessage) soup_message = NULL;
g_autoptr(SoupSession) soup_session = NULL;
g_autofree gchar *server_url = NULL;
g_autoptr(GBytes) body = NULL;
guint http_status_code = 0;
/*
* Start recording the trace if in online mode, otherwise read trace to compare
* Replace "testname" with the name of the test. This needs to be unique among
* your test suite!
*/
uhm_server_start_trace (mock_server, "testname", &error);
g_assert (error == NULL);
soup_session = soup_session_new ();
/* Query actual server if online mode is activated */
if (uhm_server_get_enable_online (mock_server)) {
server_url = g_strdup ("https://jsonplaceholder.typicode.com/todos/1");
} else {
const gchar *server_address = uhm_server_get_address (mock_server);
guint server_port = uhm_server_get_port (mock_server);
server_url = g_strdup_printf ("https://%s:%u", server_address, server_port);
}
/* Set uhttpmock as soup logger so it can capture the trace */
soup_logger = soup_logger_new (SOUP_LOGGER_LOG_BODY);
soup_logger_set_printer (soup_logger, uhm_server_received_message_chunk_from_soup, mock_server, NULL);
soup_session_add_feature (soup_session, SOUP_SESSION_FEATURE (soup_logger));
/*
* Send the message - usually your application would process the
* response from uhttpmock, do further requests, etc. afterwards
*/
soup_message = soup_message_new ("GET", server_url);
g_signal_connect (soup_message, "accept-certificate", G_CALLBACK (accept_cert), NULL);
body = soup_session_send_and_read (soup_session, soup_message, NULL, NULL);
http_status_code = soup_message_get_status (soup_message);
g_assert (http_status_code == 200);
/* End the trace - in online mode this will save the trace, otherwise it'd compare it. */
uhm_server_end_trace (mock_server);
}
int
main (int argc, char **argv)
{
g_autofree gchar *cert_path = NULL;
g_autofree gchar *key_path = NULL;
g_autofree gchar *trace_path = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GFile) trace_dir = NULL;
g_autoptr(GTlsCertificate) tls_cert = NULL;
g_test_init (&argc, &argv, NULL);
/*
* Setup the mock server with the directory to write traces to.
* replace "testsuitename" with the name of the testsuite. This must
* be unique among your project!
*/
mock_server = uhm_server_new ();
trace_path = g_test_build_filename (G_TEST_DIST, "traces", "testsuitename", NULL);
trace_dir = g_file_new_for_path (trace_path);
uhm_server_set_trace_directory (mock_server, trace_dir);
/*
* Set up self signed cert for HTTPS. The cert doesn't actually need to be
* secret - it can be distributed with your program or be generated during
* build time with: openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -nodes
*/
cert_path = g_test_build_filename (G_TEST_DIST, "sslcertdir", "cert.pem", NULL);
key_path = g_test_build_filename (G_TEST_DIST, "sslcertdir", "key.pem", NULL);
tls_cert = g_tls_certificate_new_from_files (cert_path, key_path, &error);
g_assert (error == NULL);
uhm_server_set_tls_certificate (mock_server, tls_cert);
/*
* TRUE, TRUE => Logging mode, records what actual server replies
* FALSE, TRUE => Comparing mode, checks actual server still replies as expected from trace
* FALSE, FALSE => Testing mode, requests are answered by mock server as per trace
*
* As such, you usually want TRUE, TRUE for the initial recording or if you/the server changed
* something (and as such expect a legitimate change in the trace) and then FALSE, FALSE
* to actually engage the tests.
*/
uhm_server_set_enable_logging (mock_server, TRUE);
uhm_server_set_enable_online (mock_server, TRUE);
g_test_add_func ("/test/mock", test_mock);
return g_test_run ();
}
</programlisting>
</example>
<para>
This example can be compiled using:
</para>
<informalexample>
<programlisting>
gcc -o uhttpsample uhttpsample.c $(pkg-config --cflags --libs gio-2.0 glib-2.0 libsoup-3.0 libuhttpmock-0.0)
</programlisting>
</informalexample>
<para>
Afterwards, executing <code>./uhttpsample</code> will run the test suite. Initially it will
record a trace of the requests done by the test_mock function and the responses
it receives, because <code>uhm_server_set_enable_logging</code> and
<code>uhm_server_set_enable_online</code> are both set to <code>TRUE</code>. Upon setting
both of these to <code>FALSE</code>, libuhttpmock will read the trace it
created earlier and will fail the test (by returning an HTTP status code indicating failure
for the faulty query your application makes) in case something is different - e.g. because
your application queried a different URL than expected. Keep in mind that by default
libuhttpmock will only compare the URI and methods of messages, but no headers and
not the message bodies. To override this behaviour, you can connect to the
<code>compare-messages</code> signal on <code>UhmServer</code> to implement your own
custom handler for checking if messages are as expected.
</para>
</chapter>
<chapter>
<title>Testing Workflow</title>
<para>
Once you have integrated libuhttpmock into your tests, your workflow will consist out of the
following steps:
</para>
<itemizedlist>
<listitem>
<para>
<emphasis role="strong">Log</emphasis>: After doing changes in your application that
cause wanted changes in behavior (e.g. a different response after the API has changed),
you want to record a new trace. For this you enable both logging and online modes, as
described above.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Compare</emphasis>: If you want to make sure the server still
behaves as you expect (sends you the expected responses), you can disable logging and
enable the online mode. libuhttpmock will then query the actual online server, but won't
overwrite the trace file and will error out if any response from the server has changed.
</para>
</listitem>
<listitem>
<para>
<emphasis role="strong">Test</emphasis>: When running tests in CI or when releasing your
application for downstreams to package it (and run tests), you want both logging and
the online mode to be disabled. In this case libuhttpmock will mock the responses of the
actual online server. That way no internet access is required for your tests, API keys required
for testing don't have to be shipped and so on.
</para>
</listitem>
</itemizedlist>
</chapter>
</part>
<part>
<title>libuhttpmock Overview</title>
<chapter>
<title>Object Hierarchy</title>
<xi:include href="xml/tree_index.sgml"/>
</chapter>
</part>
<part>
<title>Core API</title>
<chapter>
<title>Core API</title>
<xi:include href="xml/uhm-version.xml"/>
<xi:include href="xml/uhm-server.xml"/>
<xi:include href="xml/uhm-resolver.xml"/>
</chapter>
</part>
<part>
<title>Appendices</title>
<index role="api-index-full">
<title>API Index</title>
<xi:include href="xml/api-index-full.xml"><xi:fallback/></xi:include>
</index>
<index role="api-index-deprecated">
<title>Index of deprecated symbols</title>
<xi:include href="xml/api-index-deprecated.xml"><xi:fallback/></xi:include>
</index>
<index role="0.1.0">
<title>Index of new symbols in 0.1.0</title>
<xi:include href="xml/api-index-0.1.0.xml"><xi:fallback/></xi:include>
</index>
<index role="0.3.0">
<title>Index of new symbols in 0.3.0</title>
<xi:include href="xml/api-index-0.3.0.xml"><xi:fallback/></xi:include>
</index>
<index role="0.5.0">
<title>Index of new symbols in 0.5.0</title>
<xi:include href="xml/api-index-0.5.0.xml"><xi:fallback/></xi:include>
</index>
<xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
</part>
</book>
|