Skip to content

Commit

Permalink
test(http_client): add tests for Security Schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Mar 10, 2022
1 parent 5115a56 commit 9a53546
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/http_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2022 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

import 'package:dart_wot/dart_wot.dart';
import 'package:test/test.dart';

void main() {
group('HTTP tests', () {
setUp(() {
// Additional setup goes here.
});

test('HTTP Security Schemes', () async {
const username = "username";
const password = "password";
const token = "thisIsTheMostAwesomeTokenEver!";

// TODO(JKRhb): Does not have an effect in the TD yet (and is negotiated
// automatically by http_auth instead)
const qop = "auth-int";

final thingDescriptionJson = '''
{
"@context": ["http://www.w3.org/ns/td"],
"title": "Test Thing",
"base": "https://httpbin.org",
"securityDefinitions": {
"basic_sc": {
"scheme": "basic"
},
"digest_sc": {
"scheme": "digest"
},
"bearer_sc": {
"scheme": "bearer"
}
},
"security": "basic_sc",
"properties": {
"status": {
"forms": [
{
"href": "/basic-auth/$username/$password"
}
]
},
"status2": {
"forms": [
{
"href": "/digest-auth/$qop/$username/$password",
"security": "digest_sc",
"qop": "$qop"
}
]
},
"status3": {
"forms": [
{
"href": "/bearer",
"security": "bearer_sc"
}
]
}
}
}
''';

final parsedTd = ThingDescription(thingDescriptionJson);

final servient = Servient()
..addClientFactory(HttpClientFactory())
..addCredentials("https://httpbin.org", "basic_sc",
BasicCredentials(username, password))
..addCredentials("https://httpbin.org", "digest_sc",
DigestCredentials(username, password))
..addCredentials(
"https://httpbin.org", "bearer_sc", BearerCredentials(token));
final wot = await servient.start();

final consumedThing = await wot.consume(parsedTd);
final result = await consumedThing.readProperty("status");
final value = await result.value();
expect(value, {"authenticated": true, "user": username});

final result2 = await consumedThing.readProperty("status2");
final value2 = await result2.value();
expect(value2, {"authenticated": true, "user": username});

final result3 = await consumedThing.readProperty("status3");
final value3 = await result3.value();
expect(value3, {"authenticated": true, "token": token});
});
});
}

0 comments on commit 9a53546

Please sign in to comment.