Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Merge branch 'status-fix' of https://github.com/os0x/capybara-webkit
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Aug 26, 2011
2 parents 64357e7 + 4827174 commit 457957b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
46 changes: 46 additions & 0 deletions spec/integration/session_spec.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Reset.cpp
Expand Up @@ -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));
}

36 changes: 23 additions & 13 deletions src/WebPage.cpp
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion src/WebPage.h
Expand Up @@ -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);

Expand Down Expand Up @@ -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.