Skip to content

Commit dd9e5ed

Browse files
authored
Merge pull request #212 from catalyst/issue-209-MOODLE_35_STABLE
issue #209: allow different HTTP methods for http_post_action_step
2 parents 321535c + 4055bf0 commit dd9e5ed

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

classes/steps/actions/http_post_action_step.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* HTTP Post action step class.
18+
* HTTP action step class.
1919
*
2020
* @package tool_trigger
2121
* @copyright Matt Porritt <[email protected]>
@@ -25,7 +25,7 @@
2525
namespace tool_trigger\steps\actions;
2626

2727
/**
28-
* HTTP Post action step class.
28+
* HTTP action step class.
2929
*
3030
* @package tool_trigger
3131
* @copyright Matt Porritt <[email protected]>
@@ -35,9 +35,22 @@ class http_post_action_step extends base_action_step {
3535

3636
use \tool_trigger\helper\datafield_manager;
3737

38+
/**
39+
* Supported HTTP methods.
40+
*/
41+
const SUPPORTED_HTTP_METHODS = [
42+
'POST' => 'POST',
43+
'GET' => 'GET',
44+
'PUT' => 'PUT',
45+
'DELETE' => 'DELETE',
46+
'PATCH' => 'PATCH',
47+
];
48+
3849
protected $url;
50+
protected $httpmethod;
3951
protected $headers;
4052
protected $params;
53+
private $httphandler = null;
4154

4255
/**
4356
* The fields supplied by this step.
@@ -52,6 +65,7 @@ class http_post_action_step extends base_action_step {
5265

5366
protected function init() {
5467
$this->url = $this->data['url'];
68+
$this->httpmethod = !empty($this->data['httpmethod']) ? $this->data['httpmethod'] : 'POST';
5569
$this->headers = $this->data['httpheaders'];
5670
$this->params = $this->data['httpparams'];
5771
$this->jsonencode = $this->data['jsonencode'];
@@ -81,8 +95,6 @@ public static function get_step_desc() {
8195
return get_string('httppostactionstepdesc', 'tool_trigger');
8296
}
8397

84-
private $httphandler = null;
85-
8698
/**
8799
* Kinda hacky... unit testing requires us to specify a different http handler for guzzle to use.
88100
* That's really the only reason we need this method!
@@ -143,7 +155,7 @@ public function execute($step, $trigger, $event, $stepresults) {
143155
$params = json_encode($output);
144156
}
145157

146-
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $params);
158+
$request = new \GuzzleHttp\Psr7\Request($this->httpmethod, $url, $headers, $params);
147159
$client = $this->get_http_client();
148160

149161
try {
@@ -159,7 +171,9 @@ public function execute($step, $trigger, $event, $stepresults) {
159171
if ($response->getStatusCode() != $this->expectedresponse) {
160172
// If we weren't expecting this response, throw an exception.
161173
// The error will be caught and rerun.
162-
throw new \invalid_response_exception("HTTP Response code expected was {$this->expectedresponse}, received {$response->getStatusCode()}");
174+
throw new \invalid_response_exception(
175+
"HTTP Response code expected was {$this->expectedresponse}, received {$response->getStatusCode()}"
176+
);
163177
}
164178

165179
return array(true, $stepresults);
@@ -180,6 +194,11 @@ public function form_definition_extra($form, $mform, $customdata) {
180194
$mform->addRule('url', get_string('required'), 'required');
181195
$mform->addHelpButton('url', 'httpostactionurl', 'tool_trigger');
182196

197+
// HTTP method.
198+
$mform->addElement('select', 'httpmethod', get_string ('httpostmethod', 'tool_trigger'), self::SUPPORTED_HTTP_METHODS);
199+
$mform->setType('httpmethod', PARAM_TEXT);
200+
$mform->addHelpButton('httpmethod', 'httpostmethod', 'tool_trigger');
201+
183202
// Headers.
184203
$attributes = array('cols' => '50', 'rows' => '2');
185204
$mform->addElement('textarea', 'httpheaders', get_string ('httpostactionheaders', 'tool_trigger'), $attributes);

lang/en/tool_trigger.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@
120120
$string['historysettingsdesc'] = 'These settings provide control over how the history of a workflow is stored.';
121121
$string['httpostactionurl'] = 'URL';
122122
$string['httpostactionurl_help'] = 'The URL to post the data to.';
123+
$string['httpostmethod'] = 'HTTP method';
124+
$string['httpostmethod_help'] = 'HTTP method for the given request.';
123125
$string['httpostactionheaders'] = 'Headers';
124126
$string['httpostactionheaders_help'] = 'The requests headers to send.';
125127
$string['httpostactionparams'] = 'Parameters';
126128
$string['httpostactionparams_help'] = 'The parameters to send with the request.';
127-
$string['httppostactionstepname'] = 'HTTP Post';
129+
$string['httppostactionstepname'] = 'HTTP request';
128130
$string['httppostactionstepdesc'] = 'A step to allow Moodle workflows to send data to a HTTP/S endpoint.';
129131
$string['importmodaltitle'] = 'Import workflow from file';
130132
$string['importworkflow'] = 'Import a workflow';

tests/http_post_action_step_test.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,65 @@ private function make_mock_http_handler($response) {
6262
return $stack;
6363
}
6464

65+
/**
66+
* Test supported HTTP methods.
67+
*/
68+
public function test_supported_http_methods() {
69+
$expected = [
70+
'POST' => 'POST',
71+
'GET' => 'GET',
72+
'PUT' => 'PUT',
73+
'DELETE' => 'DELETE',
74+
'PATCH' => 'PATCH',
75+
];
76+
$this->assertSame($expected, \tool_trigger\steps\actions\http_post_action_step::SUPPORTED_HTTP_METHODS);
77+
}
78+
79+
/**
80+
* Test that POST method is set as default if no httpmethod set for the step class.
81+
* This is to make sure that steps created before httpmethod was introduced will get it by default.
82+
*/
83+
public function test_if_httpmethod_is_not_set_post_method_set_as_default() {
84+
$stepsettings = [
85+
'url' => 'http://http_post_action_step.example.com',
86+
'httpheaders' => 'My-Special-Header: {headervalue}',
87+
'httpparams' => '',
88+
'jsonencode' => '0'
89+
];
90+
91+
$step = new \tool_trigger\steps\actions\http_post_action_step(json_encode($stepsettings));
92+
93+
$reflector = new \ReflectionClass(\tool_trigger\steps\actions\http_post_action_step::class);
94+
$property = $reflector->getProperty('httpmethod');
95+
$property->setAccessible(true);
96+
97+
$this->assertEquals('POST', $property->getValue($step));
98+
}
99+
100+
/**
101+
* Data provider for all supported HTTP methods.
102+
* @return array[]
103+
*/
104+
public function http_methods_data_provider(): array {
105+
return [
106+
['POST'],
107+
['GET'],
108+
['PUT'],
109+
['DELETE'],
110+
['PATCH'],
111+
];
112+
}
113+
65114
/**
66115
* Simple test, with a successful response
116+
*
117+
* @dataProvider http_methods_data_provider
118+
* @param string $httpmethod
67119
*/
68-
public function test_execute_200() {
120+
public function test_execute_200(string $httpmethod) {
69121
$stepsettings = [
70122
'url' => 'http://http_post_action_step.example.com',
123+
'httpmethod' => $httpmethod,
71124
'httpheaders' => 'My-Special-Header: {headervalue}',
72125
'httpparams' => '',
73126
'jsonencode' => '0'
@@ -89,10 +142,14 @@ public function test_execute_200() {
89142
/**
90143
* Test that we properly handle a 404 response. Guzzle will throw an exception in this
91144
* case, but the action step should catch the exception and handle it.
145+
*
146+
* @dataProvider http_methods_data_provider
147+
* @param string $httpmethod
92148
*/
93-
public function test_execute_404() {
149+
public function test_execute_404(string $httpmethod) {
94150
$stepsettings = [
95151
'url' => 'http://http_post_action_step.example.com/badurl',
152+
'httpmethod' => $httpmethod,
96153
'httpheaders' => 'My-Special-Header: {headervalue}',
97154
'httpparams' => '',
98155
'jsonencode' => '0',

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'tool_trigger';
28-
$plugin->release = 2021030408;
29-
$plugin->version = 2021030408;
28+
$plugin->release = 2021030409;
29+
$plugin->version = 2021030409;
3030
$plugin->requires = 2016052300;
3131
$plugin->supported = [35, 310];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)