forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth20CredentialsTest.cpp
More file actions
109 lines (83 loc) · 2.47 KB
/
Copy pathOAuth20CredentialsTest.cpp
File metadata and controls
109 lines (83 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// OAuth20CredentialsTest.cpp
//
// $Id$
//
// Copyright (c) 2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "OAuth20CredentialsTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/OAuth20Credentials.h"
#include "Poco/Net/NetException.h"
using Poco::Net::HTTPRequest;
using Poco::Net::OAuth20Credentials;
using Poco::Net::NotAuthenticatedException;
OAuth20CredentialsTest::OAuth20CredentialsTest(const std::string& name): CppUnit::TestCase(name)
{
}
OAuth20CredentialsTest::~OAuth20CredentialsTest()
{
}
void OAuth20CredentialsTest::testAuthorize()
{
OAuth20Credentials creds("s3cr3tt0k3n");
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
creds.authenticate(request);
std::string auth = request.get("Authorization");
assert (auth == "Bearer s3cr3tt0k3n");
}
void OAuth20CredentialsTest::testAuthorizeCustomScheme()
{
OAuth20Credentials creds("s3cr3tt0k3n", "token");
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
creds.authenticate(request);
std::string auth = request.get("Authorization");
assert (auth == "token s3cr3tt0k3n");
}
void OAuth20CredentialsTest::testExtract()
{
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
request.set("Authorization", "Bearer s3cr3tt0k3n");
OAuth20Credentials creds(request);
assert (creds.getBearerToken() == "s3cr3tt0k3n");
}
void OAuth20CredentialsTest::testExtractCustomScheme()
{
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
request.set("Authorization", "token s3cr3tt0k3n");
OAuth20Credentials creds(request, "token");
assert (creds.getBearerToken() == "s3cr3tt0k3n");
}
void OAuth20CredentialsTest::testExtractNoCreds()
{
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
try
{
OAuth20Credentials creds(request);
fail("no credentials - must throw");
}
catch (NotAuthenticatedException&)
{
}
}
void OAuth20CredentialsTest::setUp()
{
}
void OAuth20CredentialsTest::tearDown()
{
}
CppUnit::Test* OAuth20CredentialsTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OAuth20CredentialsTest");
CppUnit_addTest(pSuite, OAuth20CredentialsTest, testAuthorize);
CppUnit_addTest(pSuite, OAuth20CredentialsTest, testAuthorizeCustomScheme);
CppUnit_addTest(pSuite, OAuth20CredentialsTest, testExtract);
CppUnit_addTest(pSuite, OAuth20CredentialsTest, testExtractCustomScheme);
CppUnit_addTest(pSuite, OAuth20CredentialsTest, testExtractNoCreds);
return pSuite;
}