summaryrefslogtreecommitdiffstats
path: root/qa/t/lib/QA
diff options
context:
space:
mode:
authorDylan William Hardison <dylan@hardison.net>2016-07-01 05:32:40 +0200
committerDylan William Hardison <dylan@hardison.net>2016-09-13 15:27:19 +0200
commit5f310e5b9e7afbd5badb17178349fefaaabfacce (patch)
tree24a4973f71f38e0cd7788a25817142ab6c05ed3e /qa/t/lib/QA
parent14bcdce1dc6029c3676c3640d8148c83c14692e5 (diff)
downloadbugzilla-5f310e5b9e7afbd5badb17178349fefaaabfacce.tar.gz
bugzilla-5f310e5b9e7afbd5badb17178349fefaaabfacce.tar.xz
Bug 1283930 - Add Makefile.PL & local/lib/perl5 support to bmo/master + local symlink to data/ directory
Diffstat (limited to 'qa/t/lib/QA')
-rw-r--r--qa/t/lib/QA/REST.pm61
1 files changed, 61 insertions, 0 deletions
diff --git a/qa/t/lib/QA/REST.pm b/qa/t/lib/QA/REST.pm
new file mode 100644
index 000000000..f900cc352
--- /dev/null
+++ b/qa/t/lib/QA/REST.pm
@@ -0,0 +1,61 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+package QA::REST;
+
+use 5.10.1;
+use strict;
+use warnings;
+use autodie;
+
+use LWP::UserAgent;
+use JSON;
+use QA::Util;
+
+use parent qw(LWP::UserAgent Exporter);
+
+@QA::REST::EXPORT = qw(
+ MUST_FAIL
+ get_rest_client
+);
+
+use constant MUST_FAIL => 1;
+
+sub get_rest_client {
+ my $rest_client = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
+ bless($rest_client, 'QA::REST');
+ my $config = $rest_client->{bz_config} = get_config();
+ $rest_client->{bz_url} = $config->{browser_url} . '/' . $config->{bugzilla_installation} . '/rest/';
+ $rest_client->{bz_default_headers} = {'Accept' => 'application/json', 'Content-Type' => 'application/json'};
+ return $rest_client;
+}
+
+sub bz_config { return $_[0]->{bz_config}; }
+
+sub call {
+ my ($self, $method, $data, $http_verb, $expect_to_fail) = @_;
+ $http_verb = lc($http_verb || 'GET');
+ $data //= {};
+
+ my %args = %{ $self->{bz_default_headers} };
+ # We do not pass the API key in the URL, so that it's not logged by the web server.
+ if ($http_verb eq 'get' && $data->{api_key}) {
+ $args{'X-BUGZILLA-API-KEY'} = $data->{api_key};
+ }
+ elsif ($http_verb ne 'get') {
+ $args{Content} = encode_json($data);
+ }
+
+ my $response = $self->$http_verb($self->{bz_url} . $method, %args);
+ my $res = decode_json($response->decoded_content);
+ if ($response->is_success xor $expect_to_fail) {
+ return $res;
+ }
+ else {
+ die 'error ' . $res->{code} . ': ' . $res->{message} . "\n";
+ }
+}