Skip to content

Commit

Permalink
Adding a unit test for HTTPRequest
Browse files Browse the repository at this point in the history
Changing HTTPRequest constructor to take an
HTTPClient instance as parameter. The idea is to
have an easy way to pass an HTTPClient mock to be
able to test it properly.
Adding FakeIt header only mocking library to
making unit test that requires mocks easier to
implement.
Adding a simple HTTPRequest unit test that makes
use of FakeIt to assess how useful could be to
testing Godot.
  • Loading branch information
pafuent committed Aug 8, 2024
1 parent eabeafd commit 5018086
Show file tree
Hide file tree
Showing 8 changed files with 10,267 additions and 3 deletions.
4 changes: 2 additions & 2 deletions scene/main/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,8 @@ void HTTPRequest::_bind_methods() {
BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
}

HTTPRequest::HTTPRequest() {
client = Ref<HTTPClient>(HTTPClient::create());
HTTPRequest::HTTPRequest(const Ref<HTTPClient> &p_client) {
client = (p_client == nullptr) ? Ref<HTTPClient>(HTTPClient::create()) : p_client;
tls_options = TLSOptions::client();
timer = memnew(Timer);
timer->set_one_shot(true);
Expand Down
2 changes: 1 addition & 1 deletion scene/main/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class HTTPRequest : public Node {

void set_tls_options(const Ref<TLSOptions> &p_options);

HTTPRequest();
HTTPRequest(const Ref<HTTPClient> &p_client = Ref<HTTPClient>());
};

VARIANT_ENUM_CAST(HTTPRequest::Result);
Expand Down
95 changes: 95 additions & 0 deletions tests/core/io/test_http_client_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**************************************************************************/
/* test_http_client_mock.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_HTTP_CLIENT_MOCK_H
#define TEST_HTTP_CLIENT_MOCK_H

#include "core/io/http_client.h"

class HTTPClientMock : public HTTPClient {
public:
Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override {
return Error();
}
Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override {
return Error();
}

void set_connection(const Ref<StreamPeer> &p_connection) override {
}
Ref<StreamPeer> get_connection() const override {
return nullptr;
}

void close() override {
}

Status get_status() const override {
return Status();
}

bool has_response() const override {
return bool();
}
bool is_response_chunked() const override {
return bool();
}
int get_response_code() const override {
return int();
}
Error get_response_headers(List<String> *r_response) override {
return Error();
}
int64_t get_response_body_length() const override {
return int64_t();
}

PackedByteArray read_response_body_chunk() override {
return PackedByteArray();
}

void set_blocking_mode(bool p_enable) override {
}
bool is_blocking_mode_enabled() const override {
return bool();
}

void set_read_chunk_size(int p_size) override {
}
int get_read_chunk_size() const override {
return int();
}

Error poll() override {
return Error();
}
};

#endif // TEST_HTTP_CLIENT_MOCK_H
61 changes: 61 additions & 0 deletions tests/scene/test_http_request.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**************************************************************************/
/* test_http_request.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_HTTP_REQUEST_H
#define TEST_HTTP_REQUEST_H

#include "scene/main/http_request.h"
#include "tests/core/io/test_http_client_mock.h"
#include "tests/test_macros.h"

#include "thirdparty/fakeit/fakeit.hpp"

namespace TestHTTPRequest {

TEST_CASE("[Network][HTTPRequest] Set download chunk size") {
HTTPClientMock *http_client = memnew(HTTPClientMock);
HTTPRequest *http_request = memnew(HTTPRequest(Ref<HTTPClient>(http_client)));

fakeit::Mock<HTTPClient> *spy = new fakeit::Mock<HTTPClient>(*http_client);
fakeit::When(Method(*spy, get_status)).AlwaysReturn(HTTPClient::STATUS_DISCONNECTED);
fakeit::Spy(Method(*spy, set_read_chunk_size));

int expected_value = 42;
http_request->set_download_chunk_size(expected_value);

fakeit::Verify(Method(*spy, set_read_chunk_size).Using(expected_value));

delete spy;
memdelete(http_request);
}

} // namespace TestHTTPRequest

#endif // TEST_HTTP_REQUEST_H
1 change: 1 addition & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#include "tests/scene/test_curve_2d.h"
#include "tests/scene/test_curve_3d.h"
#include "tests/scene/test_gradient.h"
#include "tests/scene/test_http_request.h"
#include "tests/scene/test_image_texture.h"
#include "tests/scene/test_image_texture_3d.h"
#include "tests/scene/test_instance_placeholder.h"
Expand Down
12 changes: 12 additions & 0 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ Files extracted from upstream source:
```
- `AUTHORS.txt` and `LICENSE.txt`

## fakeit

- Upstream: https://github.com/eranpeer/FakeIt
- Version: 2.4.0 (cb39d8a053876f74dfeed66dd335d3041f142095, 2023)
- License: MIT

Files extracted from upstream source:

- `fakeit.hpp`
- `LICENSE`


## fonts

- `DroidSans*.woff2`:
Expand Down
20 changes: 20 additions & 0 deletions thirdparty/fakeit/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Eran Pe'er

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 5018086

Please sign in to comment.