2024-09-23 01:44:59 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_core/core.dart';
|
|
|
|
import 'package:platform_core/http.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:http_parser/http_parser.dart';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_mocking/mocking.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
2024-09-23 04:39:29 +00:00
|
|
|
var app = Protevus();
|
|
|
|
var http = ProtevusHttp(app);
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
app.get('/default', (req, res) => res.jsonp({'foo': 'bar'}));
|
|
|
|
|
|
|
|
app.get('/callback',
|
|
|
|
(req, res) => res.jsonp({'foo': 'bar'}, callbackName: 'doIt'));
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/contentType',
|
|
|
|
(req, res) =>
|
|
|
|
res.jsonp({'foo': 'bar'}, contentType: MediaType('foo', 'bar')));
|
|
|
|
|
|
|
|
Future<MediaType> getContentType(String path) async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri(path: '/$path'));
|
|
|
|
await rq.close();
|
|
|
|
await http.handleRequest(rq);
|
|
|
|
return MediaType.parse(rq.response.headers.contentType.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getText(String path) async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri(path: '/$path'));
|
|
|
|
await rq.close();
|
|
|
|
await http.handleRequest(rq);
|
|
|
|
return await rq.response.transform(utf8.decoder).join();
|
|
|
|
}
|
|
|
|
|
|
|
|
test('default', () async {
|
|
|
|
var response = await getText('default');
|
|
|
|
var contentType = await getContentType('default');
|
|
|
|
expect(response, r'callback({"foo":"bar"})');
|
|
|
|
expect(contentType.mimeType, 'application/javascript');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('callback', () async {
|
|
|
|
var response = await getText('callback');
|
|
|
|
var contentType = await getContentType('callback');
|
|
|
|
expect(response, r'doIt({"foo":"bar"})');
|
|
|
|
expect(contentType.mimeType, 'application/javascript');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('content type', () async {
|
|
|
|
var response = await getText('contentType');
|
|
|
|
var contentType = await getContentType('contentType');
|
|
|
|
expect(response, r'callback({"foo":"bar"})');
|
|
|
|
expect(contentType.mimeType, 'foo/bar');
|
|
|
|
});
|
|
|
|
}
|