@@ -291,6 +291,74 @@ SUITE(oauth2_tests)
291291 VERIFY_ARE_EQUAL (U (" done" ), m_oauth2_config.token ().access_token ());
292292 }
293293
294+ TEST_FIXTURE (oauth2_test_setup, oauth2_token_from_client_credentials)
295+ {
296+ VERIFY_IS_FALSE (m_oauth2_config.is_enabled ());
297+
298+ m_oauth2_config.set_user_agent (U (" test_user_agent" ));
299+
300+ // Fetch using HTTP Basic authentication.
301+ {
302+ m_scoped.server ()->next_request ().then ([](test_request* request) {
303+ VERIFY_ARE_EQUAL (request->m_method , methods::POST);
304+
305+ VERIFY_IS_TRUE (is_application_x_www_form_urlencoded (request));
306+
307+ VERIFY_ARE_EQUAL (
308+ U (" Basic MTIzQUJDOjQ1NkRFRg==" ),
309+ request->m_headers [header_names::authorization]);
310+
311+ VERIFY_ARE_EQUAL (
312+ to_body_data (U (" grant_type=client_credentials" )),
313+ request->m_body );
314+
315+ VERIFY_ARE_EQUAL (
316+ U (" test_user_agent" ),
317+ get_request_user_agent (request));
318+
319+ std::map<utility::string_t , utility::string_t > headers;
320+ headers[header_names::content_type] = mime_types::application_json;
321+ request->reply (
322+ status_codes::OK, U (" " ), headers, " {\" access_token\" :\" xyzzy123\" ,\" token_type\" :\" bearer\" }" );
323+ });
324+
325+ m_oauth2_config.token_from_client_credentials ().wait ();
326+ VERIFY_ARE_EQUAL (U (" xyzzy123" ), m_oauth2_config.token ().access_token ());
327+ VERIFY_IS_TRUE (m_oauth2_config.is_enabled ());
328+ }
329+
330+ // Fetch using client key & secret in request body (x-www-form-urlencoded).
331+ {
332+ m_scoped.server ()->next_request ().then ([](test_request* request) {
333+ VERIFY_IS_TRUE (is_application_x_www_form_urlencoded (request));
334+
335+ VERIFY_ARE_EQUAL (U (" " ), request->m_headers [header_names::authorization]);
336+
337+ VERIFY_ARE_EQUAL (
338+ to_body_data (U (" grant_type=client_credentials&client_id=123ABC&client_secret=456DEF" )),
339+ request->m_body );
340+
341+ VERIFY_ARE_EQUAL (U (" test_user_agent" ), get_request_user_agent (request));
342+
343+ std::map<utility::string_t , utility::string_t > headers;
344+ headers[header_names::content_type] = mime_types::application_json;
345+ request->reply (
346+ status_codes::OK, U (" " ), headers, " {\" access_token\" :\" xyzzy123\" ,\" token_type\" :\" bearer\" }" );
347+ });
348+
349+ m_oauth2_config.set_token (oauth2_token ()); // Clear token.
350+ VERIFY_IS_FALSE (m_oauth2_config.is_enabled ());
351+
352+ m_oauth2_config.set_http_basic_auth (false );
353+ m_oauth2_config.token_from_client_credentials ().wait ();
354+
355+ VERIFY_ARE_EQUAL (
356+ U (" xyzzy123" ),
357+ m_oauth2_config.token ().access_token ());
358+ VERIFY_IS_TRUE (m_oauth2_config.is_enabled ());
359+ }
360+ }
361+
294362 TEST_FIXTURE (oauth2_test_setup, oauth2_bearer_token)
295363 {
296364 m_oauth2_config.set_token (oauth2_token (U (" 12345678" )));
0 commit comments