-
-
Notifications
You must be signed in to change notification settings - Fork 20.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
10,267 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.