Skip to content

Commit 441ba33

Browse files
author
Christian Cordiviola
committed
fix wpt_batch script
1 parent 70204f1 commit 441ba33

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

batchtool/wpt_batch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python2.6
1+
#!/usr/bin/python3
22
#
33
# Copyright 2010 Google Inc. All Rights Reserved.
44

@@ -95,11 +95,9 @@ def RunBatch(options):
9595
test_params['tcpdump'] = options.tcpdump
9696
if options.script:
9797
test_params['script'] = open(options.script, 'rb').read()
98-
if options.key:
99-
test_params['k'] = options.key
10098

10199
requested_urls = wpt_batch_lib.ImportUrls(options.urlfile)
102-
id_url_dict = wpt_batch_lib.SubmitBatch(requested_urls, test_params,
100+
id_url_dict = wpt_batch_lib.SubmitBatch(requested_urls, test_params, options.key,
103101
options.server)
104102

105103
submitted_urls = set(id_url_dict.values())

batchtool/wpt_batch_lib.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python2.6
1+
#!/usr/bin/python3
22
#
33
# Copyright 2011 Google Inc. All Rights Reserved.
44

@@ -13,12 +13,12 @@
1313

1414
__author__ = '[email protected] (Qi Zhao)'
1515

16-
import re
17-
import urllib
16+
import urllib.request
17+
import urllib.parse
1818
from xml.dom import minidom
1919

2020

21-
def __LoadEntity(url, urlopen=urllib.urlopen):
21+
def __LoadEntity(url, apiKey, urlopen=urllib.request.urlopen):
2222
"""A helper function to load an entity such as an URL.
2323
2424
Args:
@@ -28,7 +28,15 @@ def __LoadEntity(url, urlopen=urllib.urlopen):
2828
Returns:
2929
The response message
3030
"""
31-
response = urlopen(url)
31+
headers = {}
32+
33+
if apiKey:
34+
headers = {
35+
"X-WPT-API-KEY" : apiKey
36+
}
37+
38+
request = urllib.request.Request(url, headers=headers)
39+
response = urlopen(request)
3240
return response
3341

3442

@@ -44,14 +52,14 @@ def ImportUrls(url_filename):
4452
url_list = []
4553
for line in open(url_filename, 'rb'):
4654
# Remove newline and trailing whitespaces
47-
url = line.rstrip(' \r\n')
55+
url = line.rstrip(b' \r\n')
4856
if url:
4957
url_list.append(url)
5058
return url_list
5159

5260

53-
def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
54-
urlopen=urllib.urlopen):
61+
def SubmitBatch(url_list, test_params, apiKey, server_url='http://www.webpagetest.org/',
62+
urlopen=urllib.request.urlopen,):
5563
"""Submit the tests to WebPageTest server.
5664
5765
Args:
@@ -67,8 +75,8 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
6775
id_url_dict = {}
6876
for url in url_list:
6977
test_params['url'] = url
70-
request = server_url + 'runtest.php?%s' % urllib.urlencode(test_params)
71-
response = __LoadEntity(request, urlopen)
78+
request = server_url + 'runtest.php?%s' % urllib.parse.urlencode(test_params)
79+
response = __LoadEntity(request, apiKey, urlopen)
7280
return_code = response.getcode()
7381
if return_code == 200:
7482
dom = minidom.parseString(response.read())
@@ -81,7 +89,7 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
8189

8290

8391
def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
84-
urlopen=urllib.urlopen):
92+
urlopen=urllib.request.urlopen):
8593
"""Check the status of tests.
8694
8795
Args:
@@ -105,7 +113,7 @@ def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
105113

106114

107115
def GetXMLResult(test_ids, server_url='http://www.webpagetest.org/',
108-
urlopen=urllib.urlopen):
116+
urlopen=urllib.request.urlopen):
109117
"""Obtain the test result in XML format.
110118
111119
Args:

www/src/RequestContext.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ public function getApiKeyInUse(): string
169169
if (!empty($matching_headers)) {
170170
$user_api_key = array_values($matching_headers)[0];
171171
}
172-
$this->api_key_in_use = $user_api_key ?? "";
172+
173+
$this->api_key_in_use = $user_api_key;
173174
}
174175

175-
return $this->api_key_in_use ?? "";
176+
return $this->api_key_in_use;
176177
}
177178
}

0 commit comments

Comments
 (0)