Skip to content

Commit

Permalink
Merge pull request #71 from EA31337/feature/web-class
Browse files Browse the repository at this point in the history
Adds Web class [GH-13]
  • Loading branch information
kenorb committed Jun 17, 2020
2 parents 5f7bc1e + 4fa1969 commit 7aa2a02
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ env:
- CMD="docker-compose $DARGS run TickerTest"
- CMD="docker-compose $DARGS run TimerTest"
- CMD="docker-compose $DARGS run TradeTest"
# - CMD="docker-compose $DARGS run WebTest" # @fixme: GH-13
before_script:
- cd tests
script:
Expand Down
92 changes: 92 additions & 0 deletions Web.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2019, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

// Properties.
#property strict

// Prevents processing this includes file for the second time.
#ifndef WEB_MQH
#define WEB_MQH

struct WebRequestParams {
string method;
string url;
string headers;
string cookie;
string referer;
int timeout;
WebRequestParams(string _m, string _u, string _h = NULL, int _t = 500)
: method(_m), url(_u), headers(_h), timeout(_t) {};
WebRequestParams(string _m, string _u, string _c, string _r = NULL, int _t = 500)
: method(_m), url(_u), cookie(_c), referer(_r), timeout(_t) {};
WebRequestParams(string _u, string _h = NULL, int _t = 500)
: method("GET"), url(_u), headers(_h), timeout(_t) {};
};

struct WebRequestResult {
char data[];
char result[];
int error;
int http_code; // HTTP server response code or -1 for an error.
string headers;
};

/**
* Implements Web class.
*/
class Web {

private:

public:

/**
* Class constructor.
*/
Web() {
}

/**
* Web request.
*
* Note: Make sure to add URL to the list of allowed URLs in the Terminal.
*/
WebRequestResult Request(const WebRequestParams &_rp) {
ResetLastError();
WebRequestResult _res;
if (StringLen(_rp.headers) > 0) {
_res.http_code = WebRequest(
_rp.method, _rp.url, _rp.headers, _rp.timeout,
_res.data, _res.result, _res.headers);
}
else {
int _size = 0; // @fixme: data[] array size in bytes.
_res.http_code = WebRequest(
_rp.method, _rp.url, _rp.cookie, _rp.referer, _rp.timeout,
_res.data, _size, _res.result, _res.headers);
}
_res.error = GetLastError();
return _res;
}

};
#endif
28 changes: 28 additions & 0 deletions tests/WebTest.mq4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2019, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @file
* Test functionality of Web class.
*/

// Includes.
#include "WebTest.mq5"
67 changes: 67 additions & 0 deletions tests/WebTest.mq5
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2019, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+

/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* @file
* Test functionality of Web class.
*/

// Includes.
#include "../Web.mqh"
#include "../Test.mqh"
#include "../Terminal.mqh"

// Properties.
#property strict

/**
* Implements OnInit().
*/
int OnInit() {
// Test 1.
Web *web1 = new Web();
WebRequestParams req1("http://example.com");
WebRequestResult _res1;
_res1 = web1.Request(req1);
if (_res1.http_code > 0) {
PrintFormat("Web1: HTTP Response Code: %d", _res1.http_code);
PrintFormat("Web1: Headers: %s", _res1.headers);
} else {
PrintFormat("%s(): Error: Code: %d, Message: %s", __FUNCTION__, _res1.error, Terminal::GetErrorText(_res1.error));
return (INIT_FAILED);
}
delete web1;

// Test 2.
Web *web2 = new Web();
WebRequestParams req2("https://example.com");
WebRequestResult _res2;
_res2 = web2.Request(req2);
if (_res2.http_code > 0) {
PrintFormat("Web1: HTTP Response Code: %d", _res2.http_code);
PrintFormat("Web1: Headers: %s", _res2.headers);
} else {
PrintFormat("%s(): Error: Code: %d, Message: %s", __FUNCTION__, _res2.error, Terminal::GetErrorText(_res2.error));
return (INIT_FAILED);
}

return (INIT_SUCCEEDED);
}
7 changes: 7 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ services:
BT_DAYS: 10-12
BT_MONTHS: 1
OPT_VERBOSE: 1
WebTest:
command: run_backtest -s WebTest.mq4
image: ea31337/ea-tester:dev
volumes:
- ../:/opt/src
environment:
EA_WHITELIST_URLS: http://example.com/;https://example.com/

0 comments on commit 7aa2a02

Please sign in to comment.