Skip to content

Commit

Permalink
- fix issue with token injection.
Browse files Browse the repository at this point in the history
- add the missing classes from the previous commit.
- add some tests
  • Loading branch information
mustafa96m committed Jul 22, 2020
1 parent 0d47c65 commit 3b52ff3
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 24 deletions.
31 changes: 12 additions & 19 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:example/main.dart';
import 'package:miswag_auth/miswag_auth.dart';


void main() {
Authentication.setup(
baseUrl: "https://jsonbin.io/", loginPath: "5ebcfb3a47a2266b14784142");
testWidgets('testing app started..', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(SampleApp());

// Verify that our counter starts at 0.
final launchBtn = find.text('Launch App');
expect(launchBtn, findsOneWidget);
await tester.tap(launchBtn);

});
// Authentication.setup(
// baseUrl: "https://jsonbin.io/", loginPath: "5ebcfb3a47a2266b14784142");
// testWidgets('testing app started..', (WidgetTester tester) async {
// // Build our app and trigger a frame.
// await tester.pumpWidget(SampleApp());
//
// // Verify that our counter starts at 0.
// final launchBtn = find.text('Launch App');
// expect(launchBtn, findsOneWidget);
// await tester.tap(launchBtn);
//
// });
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AuthenticationInterceptors extends InterceptorsWrapper {
if (this.client.authOptions.tokenType == TokenType.Bearer) {
options.headers.addAll({
this.client.authOptions.customAuthHeader:
"${this.client.authOptions.tokenType} ${this.client.authentication[this.client.authOptions.tokenKey]}"
"Bearer ${this.client.authentication[this.client.authOptions.tokenKey]}"
});
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/misc/client/interceptors/debug_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import 'package:dio/dio.dart';
class DebugInterceptor extends InterceptorsWrapper {
@override
Future onRequest(RequestOptions options) {
print("REQUEST[${options?.method}] => PATH: ${options?.path}");
print("[${options?.method} ${options?.path}]");
return super.onRequest(options);
}

@override
Future onResponse(Response response) {
print(
"RESPONSE[${response?.statusCode}] => PATH: ${response?.request?.path}");
"[${response?.statusCode} ${response?.request?.path}] DATA => ${response.data}");
return super.onResponse(response);
}

@override
Future onError(DioError err) {
print("ERROR[${err?.response?.statusCode}] => PATH: ${err?.request?.path}");
print(
"ERROR[${err?.response?.statusCode} ${err?.request?.path}] DATA => ${err?.response?.data}");
return super.onError(err);
}
}
51 changes: 51 additions & 0 deletions lib/models/error_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// To parse this JSON data, do
//
// final errorMessage = errorMessageFromJson(jsonString);

import 'dart:convert';

class ErrorMessage {
final String message;
final Errors errors;

ErrorMessage({
this.message,
this.errors,
});

factory ErrorMessage.fromJson(String str) =>
ErrorMessage.fromMap(json.decode(str) as Map<String, dynamic>);

String toJson() => json.encode(toMap());

factory ErrorMessage.fromMap(Map<String, dynamic> json) => ErrorMessage(
message: json["message"] as String,
errors: json["errors"] == null
? null
: Errors.fromMap(json["errors"] as Map<String, dynamic>),
);

Map<String, dynamic> toMap() => {
"message": message,
"errors": errors.toMap(),
};
}

class Errors {
final Map<String, dynamic> fieldsReasons;

Errors({
this.fieldsReasons,
});

factory Errors.fromJson(String str) =>
Errors.fromMap(json.decode(str) as Map<String, dynamic>);

String toJson() => json.encode(toMap());

factory Errors.fromMap(Map<String, dynamic> json) => Errors(
fieldsReasons: json,
);

Map<String, dynamic> toMap() => fieldsReasons;
}
115 changes: 115 additions & 0 deletions lib/models/pagination_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'dart:convert';

typedef ItemsCreator<S> = List<S> Function(List<dynamic> json);

class PaginationResponse<T> {
ItemsCreator<T> creator;
final dynamic rawData;
final int currentPage;
List<T> data = [];
final String firstPageUrl;
final int from;
final int lastPage;
final String lastPageUrl;
final String nextPageUrl;
final String path;
final int perPage;
final String prevPageUrl;
final int to;
final int total;

PaginationResponse(
{this.rawData,
this.currentPage,
this.data,
this.firstPageUrl,
this.from,
this.lastPage,
this.lastPageUrl,
this.nextPageUrl,
this.path,
this.perPage,
this.prevPageUrl,
this.to,
this.total,
this.creator}) {
final List<T> items = creator(rawData as List<dynamic>);
data = items;
}

PaginationResponse copyWith({
int currentPage,
final List<T> data,
String firstPageUrl,
int from,
int lastPage,
String lastPageUrl,
String nextPageUrl,
String path,
int perPage,
String prevPageUrl,
int to,
int total,
}) =>
PaginationResponse(
currentPage: currentPage ?? this.currentPage,
data: data ?? this.data,
firstPageUrl: firstPageUrl ?? this.firstPageUrl,
from: from ?? this.from,
lastPage: lastPage ?? this.lastPage,
lastPageUrl: lastPageUrl ?? this.lastPageUrl,
nextPageUrl: nextPageUrl ?? this.nextPageUrl,
path: path ?? this.path,
perPage: perPage ?? this.perPage,
prevPageUrl: prevPageUrl ?? this.prevPageUrl,
to: to ?? this.to,
total: total ?? this.total,
);

// factory PaginationResponse.fromJson(String str) =>
// PaginationResponse.fromMap(json.decode(str) as Map<String, dynamic>);

String toJson() => json.encode(toMap());

factory PaginationResponse.fromMap(
Map<String, dynamic> json, ItemsCreator<T> creator) {
return PaginationResponse(
currentPage:
json["current_page"] == null ? null : json["current_page"] as int,
rawData: json["data"],
firstPageUrl: json["first_page_url"] == null
? null
: json["first_page_url"] as String,
from: json["from"] == null ? null : json["from"] as int,
lastPage: json["last_page"] == null ? null : json["last_page"] as int,
lastPageUrl: json["last_page_url"] == null
? null
: json["last_page_url"] as String,
nextPageUrl: json["next_page_url"] == null
? null
: json["next_page_url"] as String,
path: json["path"] == null ? null : json["path"] as String,
perPage: json["per_page"] == null ? null : json["per_page"] as int,
prevPageUrl: json["prev_page_url"] == null
? null
: json["prev_page_url"] as String,
to: json["to"] == null ? null : json["to"] as int,
total: json["total"] == null ? null : json["total"] as int,
creator: creator);
}

Map<String, dynamic> toMap() => {
"current_page": currentPage ?? currentPage,
"data": data == null ? null : List<dynamic>.from(data.map((x) => x)),
"first_page_url": firstPageUrl ?? firstPageUrl,
"from": from ?? from,
"last_page": lastPage ?? lastPage,
"last_page_url": lastPageUrl ?? lastPageUrl,
"next_page_url": nextPageUrl,
"path": path ?? path,
"per_page": perPage ?? perPage,
"prev_page_url": prevPageUrl,
"to": to ?? to,
"total": total ?? total,
};
}
29 changes: 28 additions & 1 deletion test/miswag_auth_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:miswag_auth/misc/client/api_client.dart';
import 'package:miswag_auth/models/auth_options.dart';

void main() {
test('adds one to input values', () {
test('test auth injection into headers', () async {
final AuthOptions options = AuthOptions(
baseUrl: "https://api.jsonbin.io/",
loginPath: "5ebcfb3a47a2266b14784142",
tokenType: TokenType.Bearer);
ApiClient api = ApiClient(options, debug: true);

api.authentication = {"token": "testing"};

final res = await api.client.get('/');

expect(res.request.headers[options.customAuthHeader], "Bearer testing");
});

test('test auth injection into GET', () async {
final AuthOptions options = AuthOptions(
baseUrl: "https://api.jsonbin.io/",
loginPath: "5ebcfb3a47a2266b14784142",
tokenType: TokenType.GET);
ApiClient api = ApiClient(options, debug: true);

api.authentication = {"token": "testing"};

final res = await api.client.get('/');

expect(res.request.queryParameters[options.tokenKey], "testing");
});
}

0 comments on commit 3b52ff3

Please sign in to comment.