Browse files

Merge branch 'status-fix' of https://github.com/os0x/capybara-webkit

  • Loading branch information...
2 parents 64357e7 + 4827174 commit 457957b61aa05b7b9416daead8c0747be3a85e3d @mike-burns mike-burns committed Aug 26, 2011
Showing with 74 additions and 15 deletions.
  1. +46 −0 spec/integration/session_spec.rb
  2. +2 −1 src/Reset.cpp
  3. +23 −13 src/WebPage.cpp
  4. +3 −1 src/WebPage.h
View
46 spec/integration/session_spec.rb
@@ -79,6 +79,52 @@
subject.click_button('ボタン')
end
end
+
+ context "response headers with status code" do
+ before(:all) do
+ @app = lambda do |env|
+ params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
+ if params["img"] == "true"
+ body = 'not found'
+ return [404, { 'Content-Type' => 'image/gif', 'Content-Length' => body.length.to_s }, [body]]
+ end
+ body = <<-HTML
+ <html>
+ <body>
+ <img src="?img=true">
+ </body>
+ </html>
+ HTML
+ [200,
+ { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s, 'X-Capybara' => 'WebKit'},
+ [body]]
+ end
+ end
+
+ it "should get status code" do
+ subject.visit '/'
+ subject.status_code.should == 200
+ end
+
+ it "should reset status code" do
+ subject.visit '/'
+ subject.status_code.should == 200
+ subject.reset!
+ subject.status_code.should == 0
+ end
+
+ it "should get response headers" do
+ subject.visit '/'
+ subject.response_headers['X-Capybara'].should == 'WebKit'
+ end
+
+ it "should reset response headers" do
+ subject.visit '/'
+ subject.response_headers['X-Capybara'].should == 'WebKit'
+ subject.reset!
+ subject.response_headers['X-Capybara'].should == nil
+ end
+ end
end
describe Capybara::Session, "with TestApp" do
View
3 src/Reset.cpp
@@ -11,8 +11,9 @@ void Reset::start(QStringList &arguments) {
page()->triggerAction(QWebPage::Stop);
page()->currentFrame()->setHtml("<html><body></body></html>");
page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
- page()->setNetworkAccessManager(new NetworkAccessManager());
+ page()->setCustomNetworkAccessManager();
page()->setUserAgent(NULL);
+ page()->resetResponseHeaders();
emit finished(new Response(true));
}
View
36 src/WebPage.cpp
@@ -9,17 +9,20 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
setUserStylesheet();
m_loading = false;
-
- NetworkAccessManager *manager = new NetworkAccessManager();
- this->setNetworkAccessManager(manager);
- connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
+ this->setCustomNetworkAccessManager();
connect(this, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
connect(this, SIGNAL(frameCreated(QWebFrame *)),
this, SLOT(frameCreated(QWebFrame *)));
}
+void WebPage::setCustomNetworkAccessManager() {
+ NetworkAccessManager *manager = new NetworkAccessManager();
+ this->setNetworkAccessManager(manager);
+ connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
+}
+
void WebPage::loadJavascript() {
QResource javascript(":/capybara.js");
if (javascript.isCompressed()) {
@@ -168,20 +171,27 @@ QString WebPage::getLastAttachedFileName() {
}
void WebPage::replyFinished(QNetworkReply *reply) {
- QStringList headers;
- lastStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- QList<QByteArray> list = reply->rawHeaderList();
+ if (reply->url() == this->currentFrame()->url()) {
+ QStringList headers;
+ m_lastStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
+ QList<QByteArray> list = reply->rawHeaderList();
- int length = list.size();
- for(int i = 0; i < length; i++) {
- headers << list.at(i)+": "+reply->rawHeader(list.at(i));
- }
+ int length = list.size();
+ for(int i = 0; i < length; i++) {
+ headers << list.at(i)+": "+reply->rawHeader(list.at(i));
+ }
- m_pageHeaders = headers.join("\n");
+ m_pageHeaders = headers.join("\n");
+ }
}
int WebPage::getLastStatus() {
- return lastStatus;
+ return m_lastStatus;
+}
+
+void WebPage::resetResponseHeaders() {
+ m_lastStatus = 0;
+ m_pageHeaders = QString();
}
QString WebPage::pageHeaders() {
View
4 src/WebPage.h
@@ -11,6 +11,8 @@ class WebPage : public QWebPage {
QString userAgentForUrl(const QUrl &url ) const;
void setUserAgent(QString userAgent);
int getLastStatus();
+ void resetResponseHeaders();
+ void setCustomNetworkAccessManager();
bool render(const QString &fileName);
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
@@ -38,7 +40,7 @@ class WebPage : public QWebPage {
QString getLastAttachedFileName();
void loadJavascript();
void setUserStylesheet();
- int lastStatus;
+ int m_lastStatus;
QString m_pageHeaders;
};

0 comments on commit 457957b

Please sign in to comment.