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