Skip to content

Commit

Permalink
Added browser.authenticate for http basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
halogenandtoast committed Jun 15, 2012
1 parent 6535c1b commit 5b5067f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/capybara/driver/webkit/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def initialize(connection)
@connection = connection
end

def authenticate(username, password)
command("Authenticate", username, password)
end

def visit(url)
command "Visit", url
end
Expand Down
27 changes: 27 additions & 0 deletions spec/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'capybara/driver/webkit'
require 'base64'

describe Capybara::Driver::Webkit do
subject { Capybara::Driver::Webkit.new(@app, :browser => $webkit_browser) }
Expand Down Expand Up @@ -1711,4 +1712,30 @@ def which_for(character)
end.to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end
end

describe "basic auth" do
before(:all) do
@app = lambda do |env|
if env["REQUEST_PATH"] == "/hello/world"
[200, {"Content-Type" => "text/html", "Content-Length" => "0"}, [""]]
else
if env["HTTP_AUTHORIZATION"]
header = env["HTTP_AUTHORIZATION"]
[200, {"Content-Type" => "text/html", "Content-Length" => header.length.to_s}, [header]]
else
html = "401 Unauthorized."
[401,
{"Content-Type" => "text/html", "Content-Length" => html.length.to_s, "WWW-Authenticate" => 'Basic realm="Secure Area"'},
[html]]
end
end
end
end

it "can authenticate a request" do
subject.browser.authenticate('user', 'password')
subject.visit("/")
subject.body.should include("Basic "+Base64.encode64("user:password").strip)
end
end
end
18 changes: 18 additions & 0 deletions src/Authenticate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "Authenticate.h"
#include "WebPage.h"
#include "NetworkAccessManager.h"

Authenticate::Authenticate(WebPageManager *manager, QStringList &arguments, QObject *parent) : Command(manager, arguments, parent) {
}

void Authenticate::start() {
QString username = arguments()[0];
QString password = arguments()[1];

NetworkAccessManager* networkAccessManager = qobject_cast<NetworkAccessManager*>(page()->networkAccessManager());
networkAccessManager->setUserName(username);
networkAccessManager->setPassword(password);

emit finished(new Response(true));
}

12 changes: 12 additions & 0 deletions src/Authenticate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Command.h"

class WebPage;

class Authenticate : public Command {
Q_OBJECT

public:
Authenticate(WebPageManager *manager, QStringList &arguments, QObject *parent = 0);
virtual void start();
};

1 change: 1 addition & 0 deletions src/CommandFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "GetWindowHandles.h"
#include "GetWindowHandle.h"
#include "WebPageManager.h"
#include "Authenticate.h"

CommandFactory::CommandFactory(WebPageManager *manager, QObject *parent) : QObject(parent) {
m_manager = manager;
Expand Down
16 changes: 16 additions & 0 deletions src/NetworkAccessManager.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "NetworkAccessManager.h"
#include "WebPage.h"
#include <iostream>
#include <fstream>


NetworkAccessManager::NetworkAccessManager(QObject *parent):QNetworkAccessManager(parent) {
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
}

QNetworkReply* NetworkAccessManager::createRequest(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice * outgoingData = 0) {
Expand All @@ -27,3 +29,17 @@ void NetworkAccessManager::resetHeaders() {
m_headers.clear();
};

void NetworkAccessManager::setUserName(const QString &userName) {
m_userName = userName;
}

void NetworkAccessManager::setPassword(const QString &password) {
m_password = password;
}


void NetworkAccessManager::provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator) {
Q_UNUSED(reply);
authenticator->setUser(m_userName);
authenticator->setPassword(m_password);
}
7 changes: 7 additions & 0 deletions src/NetworkAccessManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ class NetworkAccessManager : public QNetworkAccessManager {
NetworkAccessManager(QObject *parent = 0);
void addHeader(QString key, QString value);
void resetHeaders();
void setUserName(const QString &userName);
void setPassword(const QString &password);

protected:
QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice * outgoingData);
QString m_userName;
QString m_password;

private:
QHash<QString, QString> m_headers;

private slots:
void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
};
1 change: 1 addition & 0 deletions src/find_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ CHECK_COMMAND(SetSkipImageLoading)
CHECK_COMMAND(WindowFocus)
CHECK_COMMAND(GetWindowHandles)
CHECK_COMMAND(GetWindowHandle)
CHECK_COMMAND(Authenticate)
2 changes: 2 additions & 0 deletions src/webkit_server.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
HEADERS = \
Authenticate.h \
IgnoreSslErrors.h \
ResizeWindow.h \
CurrentUrl.h \
Expand Down Expand Up @@ -45,6 +46,7 @@ HEADERS = \
GetWindowHandle.h \

SOURCES = \
Authenticate.cpp \
IgnoreSslErrors.cpp \
ResizeWindow.cpp \
CurrentUrl.cpp \
Expand Down

0 comments on commit 5b5067f

Please sign in to comment.