Handle unsupported content types gracefully

This commit is contained in:
Matthew Mongeau 2011-09-15 11:22:10 -04:00
parent f493b22f6b
commit 353fe860b2
8 changed files with 72 additions and 5 deletions

View File

@ -134,6 +134,20 @@ describe Capybara::Driver::Webkit do
end
end
context "css app" do
before(:all) do
body = "css"
@app = lambda do |env|
[200, {"Content-Type" => "text/css", "Content-Length" => body.length.to_s}, [body]]
end
end
it "should render unsupported content types gracefully" do
subject.visit("/")
subject.body.should =~ /css/
end
end
context "hello app" do
before(:all) do
@app = lambda do |env|

View File

@ -1,5 +1,6 @@
#include "Connection.h"
#include "WebPage.h"
#include "UnsupportedContentHandler.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
@ -28,7 +29,7 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
m_pageSuccess = true;
m_commandWaiting = false;
connect(m_socket, SIGNAL(readyRead()), this, SLOT(checkNext()));
connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
void Connection::checkNext() {

View File

@ -0,0 +1,23 @@
#include "UnsupportedContentHandler.h"
#include "WebPage.h"
#include <QNetworkReply>
UnsupportedContentHandler::UnsupportedContentHandler(WebPage *page, QNetworkReply *reply, QObject *parent) : QObject(parent) {
m_page = page;
m_reply = reply;
connect(m_reply, SIGNAL(finished()), this, SLOT(handleUnsupportedContent()));
disconnect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
}
void UnsupportedContentHandler::handleUnsupportedContent() {
QVariant contentMimeType = m_reply->header(QNetworkRequest::ContentTypeHeader);
if(!contentMimeType.isNull()) {
QByteArray text = m_reply->readAll();
m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url());
connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
m_page->loadFinished(true);
} else {
connect(m_page, SIGNAL(loadFinished(bool)), m_page, SLOT(loadFinished(bool)));
m_page->loadFinished(false);
}
}

View File

@ -0,0 +1,16 @@
#include <QObject>
class WebPage;
class QNetworkReply;
class UnsupportedContentHandler : public QObject {
Q_OBJECT
public:
UnsupportedContentHandler(WebPage *page, QNetworkReply *reply, QObject *parent = 0);
public slots:
void handleUnsupportedContent();
private:
WebPage *m_page;
QNetworkReply *m_reply;
};

View File

@ -3,7 +3,7 @@
#include "WebPage.h"
Visit::Visit(WebPage *page, QObject *parent) : Command(page, parent) {
connect(page, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
connect(page, SIGNAL(pageFinished(bool)), this, SLOT(loadFinished(bool)));
}
void Visit::start(QStringList &arguments) {

View File

@ -1,10 +1,12 @@
#include "WebPage.h"
#include "JavascriptInvocation.h"
#include "NetworkAccessManager.h"
#include "UnsupportedContentHandler.h"
#include <QResource>
#include <iostream>
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
setForwardUnsupportedContent(true);
loadJavascript();
setUserStylesheet();
@ -15,6 +17,8 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
connect(this, SIGNAL(frameCreated(QWebFrame *)),
this, SLOT(frameCreated(QWebFrame *)));
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
}
void WebPage::setCustomNetworkAccessManager() {
@ -111,8 +115,8 @@ void WebPage::loadStarted() {
}
void WebPage::loadFinished(bool success) {
Q_UNUSED(success);
m_loading = false;
emit pageFinished(success);
}
bool WebPage::isLoading() const {
@ -198,3 +202,8 @@ void WebPage::resetResponseHeaders() {
QString WebPage::pageHeaders() {
return m_pageHeaders;
}
void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
UnsupportedContentHandler *handler = new UnsupportedContentHandler(this, reply);
Q_UNUSED(handler);
}

View File

@ -25,6 +25,10 @@ class WebPage : public QWebPage {
QString pageHeaders();
void frameCreated(QWebFrame *);
void replyFinished(QNetworkReply *reply);
void handleUnsupportedContent(QNetworkReply *reply);
signals:
void pageFinished(bool);
protected:
virtual void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);

View File

@ -1,8 +1,8 @@
TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h NetworkAccessManager.h Header.h Render.h body.h Status.h Headers.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp NetworkAccessManager.cpp Header.cpp Render.cpp body.cpp Status.cpp Headers.cpp
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h NetworkAccessManager.h Header.h Render.h body.h Status.h Headers.h UnsupportedContentHandler.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp NetworkAccessManager.cpp Header.cpp Render.cpp body.cpp Status.cpp Headers.cpp UnsupportedContentHandler.cpp
RESOURCES = webkit_server.qrc
QT += network webkit
CONFIG += console