7#include <QtCore/QByteArray>
8#include <QtCore/QCoreApplication>
9#include <QtCore/QElapsedTimer>
10#include <QtCore/QEventLoop>
11#include <QtCore/QHash>
12#include <QtCore/QJsonArray>
13#include <QtCore/QJsonDocument>
14#include <QtCore/QJsonObject>
15#include <QtCore/QList>
16#include <QtCore/QTimer>
17#include <QtNetwork/QLocalSocket>
18#include <QtNetwork/QTcpSocket>
33bool waitUntil(
const std::function<
bool()> &predicate,
int timeoutMs = 5000);
42QString
resultText(
const QJsonObject &toolResult);
59 QJsonObject
request(
const QString &method,
const QJsonObject ¶ms = { },
60 int timeoutMs = 5000);
63 int sendRequest(
const QString &method,
const QJsonObject ¶ms = { });
69 void notify(
const QString &method,
const QJsonObject ¶ms = { });
72 void sendRaw(
const QByteArray &data);
75 QJsonObject
initialize(
const QString &protocolVersion = QStringLiteral(
"2025-06-18"));
78 QJsonObject
callTool(
const QString &name,
const QJsonObject &arguments = { },
79 int timeoutMs = 5000);
82 QString
callToolText(
const QString &name,
const QJsonObject &arguments = { },
83 int timeoutMs = 5000);
117 QJsonObject
json()
const {
return QJsonDocument::fromJson(
body).object(); }
119 QByteArray
header(
const char *name)
const {
return headers.value(QByteArray(name)); }
123QList<HttpResponse> rawHttp(quint16 port,
const QByteArray &request,
int count = 1,
124 int timeoutMs = 5000);
127HttpResponse http(quint16 port,
const QByteArray &method,
const QByteArray &path,
128 const QList<QPair<QByteArray, QByteArray>> &headers = { },
129 const QByteArray &body = { },
int timeoutMs = 5000);
141 QJsonObject
request(
const QString &method,
const QJsonObject ¶ms = { });
143 HttpResponse
post(
const QJsonValue &body,
144 const QList<QPair<QByteArray, QByteArray>> &extraHeaders = { });
159 SseStream(quint16 port,
const QByteArray &session);
QJsonObject request(const QString &method, const QJsonObject ¶ms={ })
POSTs a JSON-RPC request with the session header.
Definition testsupport.cpp:275
QJsonObject initialize()
Sends initialize and stores the returned session id.
Definition testsupport.cpp:261
QByteArray sessionId() const
The session id from initialize.
Definition testsupport.h:146
HttpResponse post(const QJsonValue &body, const QList< QPair< QByteArray, QByteArray > > &extraHeaders={ })
POSTs any JSON body with the session header.
Definition testsupport.cpp:285
QByteArray m_session
Mcp-Session-Id from initialize.
Definition testsupport.h:150
HttpClient(quint16 port)
Creates a client for the server on 127.0.0.1:port.
Definition testsupport.h:136
int m_nextId
Next request id.
Definition testsupport.h:151
quint16 m_port
Server port.
Definition testsupport.h:149
QLocalSocket m_socket
Connection to the server.
Definition testsupport.h:101
QList< QJsonArray > m_batches
Array responses.
Definition testsupport.h:105
QJsonObject request(const QString &method, const QJsonObject ¶ms={ }, int timeoutMs=5000)
Sends a request and waits for its response.
Definition testsupport.cpp:121
bool waitForNotification(const QString &method, int timeoutMs=3000, qsizetype from=0)
Waits until a notification with method arrives (counting from from).
Definition testsupport.cpp:168
bool isConnected() const
Whether the socket connected.
Definition testsupport.cpp:77
void sendRaw(const QByteArray &data)
Writes raw bytes to the socket.
Definition testsupport.cpp:135
int sendRequest(const QString &method, const QJsonObject ¶ms={ })
Sends a request without waiting; returns the id used.
Definition testsupport.cpp:103
QJsonObject initialize(const QString &protocolVersion=QStringLiteral("2025-06-18"))
Performs initialize + notifications/initialized.
Definition testsupport.cpp:141
QHash< int, QJsonObject > m_responses
Responses not yet collected, by id.
Definition testsupport.h:103
QList< QJsonObject > m_notifications
Messages without an id.
Definition testsupport.h:104
void notify(const QString &method, const QJsonObject ¶ms={ })
Sends a notification (no id).
Definition testsupport.cpp:126
int m_nextId
Next request id.
Definition testsupport.h:106
const QList< QJsonArray > & batches() const
Responses that arrived as JSON arrays (legacy batches).
Definition testsupport.h:92
QJsonObject waitForResponse(int id, int timeoutMs=5000)
Waits for the response to request id.
Definition testsupport.cpp:115
QLocalSocket & socket()
The underlying socket.
Definition testsupport.h:95
const QList< QJsonObject > & notifications() const
All notifications received so far.
Definition testsupport.h:89
LocalClient(const QString &name)
Connects to the local server name; check isConnected().
Definition testsupport.cpp:70
QString callToolText(const QString &name, const QJsonObject &arguments={ }, int timeoutMs=5000)
Calls a tool and returns the concatenated text of its result.
Definition testsupport.cpp:163
void onReadyRead()
Parses complete lines from the read buffer.
Definition testsupport.cpp:82
QByteArray m_buffer
Received bytes not yet split into lines.
Definition testsupport.h:102
QJsonObject callTool(const QString &name, const QJsonObject &arguments={ }, int timeoutMs=5000)
Calls a tool and returns its result object (empty on JSON-RPC error).
Definition testsupport.cpp:154
QByteArray m_buffer
Received bytes not yet parsed.
Definition testsupport.h:175
SseStream(quint16 port, const QByteArray &session)
Opens the stream for session on port.
Definition testsupport.cpp:300
bool waitForMessage(const QString &method, int timeoutMs=3000)
Waits until a message with method has arrived.
Definition testsupport.cpp:338
QList< QJsonObject > m_messages
Parsed data: payloads.
Definition testsupport.h:177
int waitForStatus(int timeoutMs=3000)
Waits for the response head; returns the status code.
Definition testsupport.cpp:332
void onReadyRead()
Parses received SSE data.
Definition testsupport.cpp:311
const QList< QJsonObject > & messages() const
Messages received so far.
Definition testsupport.h:166
QTcpSocket m_socket
Stream connection.
Definition testsupport.h:174
bool isClosed() const
Whether the server closed the stream.
Definition testsupport.h:168
int m_status
Response status, 0 until the head arrives.
Definition testsupport.h:176
A parsed HTTP response.
Definition testsupport.h:111
int status
Status code, 0 if no response arrived.
Definition testsupport.h:112
QHash< QByteArray, QByteArray > headers
Header fields, names lower-cased.
Definition testsupport.h:113
QByteArray header(const char *name) const
A header value by lower-case name.
Definition testsupport.h:119
QByteArray body
Response body.
Definition testsupport.h:114
QJsonObject json() const
The body parsed as a JSON object.
Definition testsupport.h:117
QString uniqueName(const QString &prefix)
Returns a process-unique name with the given prefix (for sockets).
Definition testsupport.cpp:49
void spin(int ms)
Runs the event loop for ms milliseconds.
Definition testsupport.cpp:42
bool waitUntil(const std::function< bool()> &predicate, int timeoutMs)
Runs the event loop until predicate is true or timeoutMs expires.
Definition testsupport.cpp:28
void reportCrashesOnStderr()
For the tests' main(): on Windows, sends the C runtime's reports (qFatal() and failed assertions in a...
Definition testsupport.cpp:17
QString resultText(const QJsonObject &toolResult)
Concatenates the text blocks of a tool result's content array.
Definition testsupport.cpp:58