Skip to content

Instantly share code, notes, and snippets.

@hleofxquotes
Created October 4, 2018 22:31
Show Gist options
  • Save hleofxquotes/d88bb3cd4b0fbd4449f892b17704ea7c to your computer and use it in GitHub Desktop.
Save hleofxquotes/d88bb3cd4b0fbd4449f892b17704ea7c to your computer and use it in GitHub Desktop.
Dart, test code, http get, json, checksum
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
void main() {
test('json http unit test', () async {
http.Response response =
await http.get('https://jsonplaceholder.typicode.com/posts/1');
expect(200, response.statusCode);
Post post = Post.fromJson(json.decode(response.body));
expect(1, post.userId);
expect(1, post.id);
expect(
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
post.title);
expect(158, post.body.length);
Digest digest = sha1.convert(utf8.encode(post.body));
expect("0c1039e91b3fd0772a39565d587189a801de7762", digest.toString());
});
}
class Post {
final int userId;
final int id;
final String title;
final String body;
Post({this.userId, this.id, this.title, this.body});
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
userId: json['userId'],
id: json['id'],
title: json['title'],
body: json['body'],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment