Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use http package #21

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions lib/src/http_client/tbdex_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

import 'package:tbdex/src/http_client/models/create_exchange_request.dart';
import 'package:tbdex/src/http_client/models/exchange.dart';
Expand All @@ -19,10 +19,10 @@ class TbdexHttpClient {
static const _jsonHeader = 'application/json';
static const _expirationDuration = Duration(minutes: 5);

static HttpClient _client = HttpClient();
static http.Client _client = http.Client();

// ignore: avoid_setters_without_getters
static set client(HttpClient client) {
static set client(http.Client client) {
_client = client;
}

Expand All @@ -35,17 +35,18 @@ class TbdexHttpClient {
final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid);
final url = Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId');

final request = await _client.getUrl(url);
request.headers.set('Authorization', 'Bearer $requestToken');
final response = await request.close();

final body = await response.transform(utf8.decoder).join();
final response = await _client.get(
url,
headers: {
'Authorization': 'Bearer $requestToken',
},
);

if (response.statusCode != 200) {
throw Exception('failed to fetch exchange: $body');
throw Exception('failed to fetch exchange: ${response.body}');
}

return Parser.parseExchange(body);
return Parser.parseExchange(response.body);
}

static Future<List<Exchange>> getExchanges(
Expand All @@ -56,17 +57,18 @@ class TbdexHttpClient {
final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid);
final url = Uri.parse('$pfiServiceEndpoint/exchanges/');

final request = await _client.getUrl(url);
request.headers.set('Authorization', 'Bearer $requestToken');
final response = await request.close();

final body = await response.transform(utf8.decoder).join();
final response = await _client.get(
url,
headers: {
'Authorization': 'Bearer $requestToken',
},
);

if (response.statusCode != 200) {
throw Exception('failed to fetch exchanges: $body');
throw Exception('failed to fetch exchanges: ${response.body}');
}

return Parser.parseExchanges(body);
return Parser.parseExchanges(response.body);
}

static Future<List<Offering>> getOfferings(
Expand All @@ -78,16 +80,13 @@ class TbdexHttpClient {
queryParameters: filter?.toJson(),
);

final request = await _client.getUrl(url);
final response = await request.close();

final body = await response.transform(utf8.decoder).join();
final response = await _client.get(url);

if (response.statusCode != 200) {
throw Exception(response);
}

return Parser.parseOfferings(body);
return Parser.parseOfferings(response.body);
}

static Future<void> createExchange(
Expand Down Expand Up @@ -130,9 +129,13 @@ class TbdexHttpClient {
final path = '/exchanges${exchangeId != null ? '/$exchangeId' : ''}';
final url = Uri.parse(pfiServiceEndpoint + path);

final request = await _client.postUrl(url);
request.headers.set('Content-Type', _jsonHeader);
final response = await request.close();
final response = await _client.post(
url,
headers: {
'Content-Type': _jsonHeader,
},
body: requestBody,
);

if (response.statusCode != 201) {
throw Exception(response);
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';

class MockHttpClient extends Mock implements http.Client {}
File renamed without changes.
163 changes: 69 additions & 94 deletions test/http_client/tbdex_http_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

import 'package:mocktail/mocktail.dart';
import 'package:tbdex/src/http_client/tbdex_http_client.dart';
import 'package:test/test.dart';

import '../test_data.dart';

class MockClient extends Mock implements HttpClient {}

class MockHttpHeaders extends Mock implements HttpHeaders {}

class MockHttpRequest extends Mock implements HttpClientRequest {}

class MockHttpResponse extends Mock implements HttpClientResponse {}
import '../helpers/mocks.dart';
import '../helpers/test_data.dart';

void main() async {
const pfiDid = 'did:web:localhost%3A8892:ingress';
Expand All @@ -23,73 +15,55 @@ void main() async {
const didDoc =
'''{"id":"did:web:localhost%3A8892:ingress","verificationMethod":[{"id":"#0","type":"JsonWebKey","controller":"did:web:localhost%3A8892:ingress","publicKeyJwk":{"kty":"OKP","crv":"Ed25519","x":"oQ6Nl6pZjDa0I2MIsPV7q7aXX7moneoIC0XprR6ull8"}}],"service":[{"id":"#pfi","type":"PFI","serviceEndpoint":["localhost:8892/ingress/pfi"]}]}''';

late MockClient client;
late MockHttpHeaders headers;

late MockHttpRequest didRequest;
late MockHttpResponse didResponse;

late MockHttpRequest clientRequest;
late MockHttpResponse clientResponse;
late MockHttpClient mockHttpClient;

await TestData.initializeDids();

group('TbdexHttpClient', () {
setUp(() {
client = MockClient();
headers = MockHttpHeaders();

didRequest = MockHttpRequest();
didResponse = MockHttpResponse();
mockHttpClient = MockHttpClient();
TbdexHttpClient.client = mockHttpClient;

clientRequest = MockHttpRequest();
clientResponse = MockHttpResponse();

TbdexHttpClient.client = client;

when(() => didResponse.statusCode).thenReturn(200);
when(() => didResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(didDoc));
when(() => didRequest.close()).thenAnswer((_) async => didResponse);
when(
() => client.getUrl(Uri.parse(didUrl)),
).thenAnswer((_) async => didRequest);
() => mockHttpClient.get(Uri.parse(didUrl)),
).thenAnswer((_) async => http.Response(didDoc, 200));
});

test('can get exchange', () async {
when(() => clientRequest.headers).thenReturn(headers);
when(() => clientResponse.statusCode).thenReturn(200);
when(() => clientResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(TestData.getExchangeResponse()));
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);
when(
() => client
.getUrl(Uri.parse('$pfiServiceEndpoint/exchanges/exchange_id')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/exchanges/1234'),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(TestData.getExchangeResponse(), 200),
);

final exchange = await TbdexHttpClient.getExchange(
TestData.aliceDid,
pfiDid,
'exchange_id',
'1234',
);

expect(exchange.length, 2);

verify(
() => client
.getUrl(Uri.parse('$pfiServiceEndpoint/exchanges/exchange_id')),
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/exchanges/1234'),
headers: any(named: 'headers'),
),
).called(1);
});

test('can get exchanges', () async {
when(() => clientRequest.headers).thenReturn(headers);
when(() => clientResponse.statusCode).thenReturn(200);
when(() => clientResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(TestData.getExchangesResponse()));
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);
when(
() => client.getUrl(Uri.parse('$pfiServiceEndpoint/exchanges/')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/exchanges/'),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(TestData.getExchangesResponse(), 200),
);

final exchanges = await TbdexHttpClient.getExchanges(
TestData.aliceDid,
Expand All @@ -99,49 +73,50 @@ void main() async {
expect(exchanges.length, 1);

verify(
() => client.getUrl(Uri.parse('$pfiServiceEndpoint/exchanges/')),
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/exchanges/'),
headers: any(named: 'headers'),
),
).called(1);
});

test('can get offerings', () async {
when(() => clientResponse.statusCode).thenReturn(200);
when(() => clientResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(TestData.getOfferingResponse()));
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);
when(
() => client.getUrl(Uri.parse('$pfiServiceEndpoint/offerings/')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
).thenAnswer(
(_) async => http.Response(TestData.getOfferingResponse(), 200),
);

final offerings = await TbdexHttpClient.getOfferings(pfiDid);

expect(offerings.length, 1);

verify(
() => client.getUrl(Uri.parse('$pfiServiceEndpoint/offerings/')),
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
).called(1);
});

test('can create exchange', () async {
final rfq = TestData.getRfq(to: pfiDid);
await rfq.sign(TestData.aliceDid);

clientRequest
.write(TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'));
when(() => clientRequest.headers).thenReturn(headers);
when(() => clientResponse.statusCode).thenReturn(201);
when(() => clientResponse.transform(utf8.decoder)).thenAnswer(
(_) => Stream.value(''),
);
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);
when(
() => client.postUrl(Uri.parse('$pfiServiceEndpoint/exchanges')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges'),
headers: any(named: 'headers'),
body: TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'),
),
).thenAnswer(
(_) async => http.Response('', 201),
);

await TbdexHttpClient.createExchange(rfq, replyTo: 'reply_to');

verify(
() => client.postUrl(
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges'),
headers: any(named: 'headers'),
body: TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'),
),
).called(1);
});
Expand All @@ -151,24 +126,23 @@ void main() async {
final exchangeId = order.metadata.exchangeId;
await order.sign(TestData.aliceDid);

clientRequest.write(TestData.getSubmitOrderRequest(order));

when(() => clientRequest.headers).thenReturn(headers);
when(() => clientResponse.statusCode).thenReturn(201);
when(() => clientResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(''));
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);

when(
() => client
.postUrl(Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitOrderRequest(order),
),
).thenAnswer(
(_) async => http.Response('', 201),
);

await TbdexHttpClient.submitOrder(order);

verify(
() => client.postUrl(
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitOrderRequest(order),
),
).called(1);
});
Expand All @@ -178,22 +152,23 @@ void main() async {
final exchangeId = close.metadata.exchangeId;
await close.sign(TestData.aliceDid);

clientRequest.write(TestData.getSubmitCloseRequest(close));
when(() => clientRequest.headers).thenReturn(headers);
when(() => clientResponse.statusCode).thenReturn(201);
when(() => clientResponse.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(''));
when(() => clientRequest.close()).thenAnswer((_) async => clientResponse);
when(
() => client
.postUrl(Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId')),
).thenAnswer((_) async => clientRequest);
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitCloseRequest(close),
),
).thenAnswer(
(_) async => http.Response('', 201),
);

await TbdexHttpClient.submitClose(close);

verify(
() => client.postUrl(
() => mockHttpClient.post(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitCloseRequest(close),
),
).called(1);
});
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/models/close_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:tbdex/src/protocol/models/message_data.dart';
import 'package:test/test.dart';
import 'package:typeid/typeid.dart';

import '../../test_data.dart';
import '../../helpers/test_data.dart';

void main() async {
await TestData.initializeDids();
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/models/message_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:tbdex/src/protocol/models/message.dart';
import 'package:test/test.dart';

import '../../test_data.dart';
import '../../helpers/test_data.dart';

void main() async {
await TestData.initializeDids();
Expand Down
Loading
Loading