зеркало из https://github.com/electron/electron.git
Add `resourceType` arg to webContents `did-get-response-details` event.
Fixes #5074 and follows @zcbenz's recommendation to expose ResourceTypeToString from atom_network_delegate publicly. Also adds testing for other arguments to the `did-get-response-details` events, since there were no existing tests for them.
This commit is contained in:
Родитель
066092abb6
Коммит
c1b1348735
|
@ -14,6 +14,7 @@
|
|||
#include "atom/browser/atom_browser_context.h"
|
||||
#include "atom/browser/atom_browser_main_parts.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
#include "atom/browser/web_contents_permission_helper.h"
|
||||
#include "atom/browser/web_contents_preferences.h"
|
||||
#include "atom/browser/web_view_guest_delegate.h"
|
||||
|
@ -587,7 +588,8 @@ void WebContents::DidGetResourceResponseStart(
|
|||
details.http_response_code,
|
||||
details.method,
|
||||
details.referrer,
|
||||
details.headers.get());
|
||||
details.headers.get(),
|
||||
ResourceTypeToString(details.resource_type));
|
||||
}
|
||||
|
||||
void WebContents::DidGetRedirectForResourceRequest(
|
||||
|
|
|
@ -10,15 +10,12 @@
|
|||
#include "base/stl_util.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
const char* ResourceTypeToString(content::ResourceType type) {
|
||||
switch (type) {
|
||||
case content::RESOURCE_TYPE_MAIN_FRAME:
|
||||
|
@ -39,6 +36,8 @@ const char* ResourceTypeToString(content::ResourceType type) {
|
|||
return "other";
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener,
|
||||
scoped_ptr<base::DictionaryValue> details) {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "net/base/net_errors.h"
|
||||
#include "net/http/http_request_headers.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
|
||||
namespace extensions {
|
||||
class URLPattern;
|
||||
|
@ -24,6 +25,8 @@ namespace atom {
|
|||
|
||||
using URLPatterns = std::set<extensions::URLPattern>;
|
||||
|
||||
const char* ResourceTypeToString(content::ResourceType type);
|
||||
|
||||
class AtomNetworkDelegate : public brightray::NetworkDelegate {
|
||||
public:
|
||||
using ResponseCallback = base::Callback<void(const base::DictionaryValue&)>;
|
||||
|
|
|
@ -101,6 +101,32 @@ describe('browser-window module', function () {
|
|||
w.loadURL('about:blank')
|
||||
})
|
||||
|
||||
it('should emit did-get-response-details event', function (done) {
|
||||
// expected {fileName: resourceType} pairs
|
||||
var expectedResources = {
|
||||
'did-get-response-details.html': 'mainFrame',
|
||||
'logo.png': 'image'
|
||||
}
|
||||
var responses = 0;
|
||||
w.webContents.on('did-get-response-details', function (event, status, newUrl, oldUrl, responseCode, method, referrer, headers, resourceType) {
|
||||
responses++
|
||||
var fileName = newUrl.slice(newUrl.lastIndexOf('/') + 1)
|
||||
var expectedType = expectedResources[fileName]
|
||||
assert(!!expectedType, `Unexpected response details for ${newUrl}`)
|
||||
assert(typeof status === 'boolean', 'status should be boolean')
|
||||
assert.equal(responseCode, 200)
|
||||
assert.equal(method, 'GET')
|
||||
assert(typeof referrer === 'string', 'referrer should be string')
|
||||
assert(!!headers, 'headers should be present')
|
||||
assert(typeof headers === 'object', 'headers should be object')
|
||||
assert.equal(resourceType, expectedType, 'Incorrect resourceType')
|
||||
if (responses === Object.keys(expectedResources).length) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
w.loadURL('file://' + path.join(fixtures, 'pages', 'did-get-response-details.html'))
|
||||
})
|
||||
|
||||
it('should emit did-fail-load event for files that do not exist', function (done) {
|
||||
w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
|
||||
assert.equal(code, -6)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<img src="../assets/logo.png" />
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче