http_mock_adapter 0.1.4 copy "http_mock_adapter: ^0.1.4" to clipboard
http_mock_adapter: ^0.1.4 copied to clipboard

outdated

HTTP mock adapter for Dio & Mockito. By simply defining requests and corresponding responses through predefined adapters, you will be able to mock requests sent via Dio.

example/main.dart

import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:test/test.dart';

void main() async {
  // How to mock with DioAdapter
  group('DioAdapter usage', () {
    // Creating dio instance for mocking.
    // For instance: you can use your own instance from injection and replace
    // dio.httpClientAdapter with mocker DioAdapter

    const path = 'https://example.com';

    test('Expects Dioadapter to mock the data', () async {
      final dio = Dio();
      final dioAdapter = DioAdapter();

      dio.httpClientAdapter = dioAdapter;
      dioAdapter
          .onGet(path)
          .reply(200,
              {'message': 'Successfully mocked GET!'}) // only use double quotes
          .onPost(path)
          .reply(200, {'message': 'Successfully mocked POST!'});

      // Making dio.get request on the path an expecting mocked response
      final getResponse = await dio.get(path);
      expect(jsonEncode({'message': 'Successfully mocked GET!'}),
          getResponse.data);

      // Making dio.post request on the path an expecting mocked response
      final postResponse = await dio.post(path);
      expect(jsonEncode({'message': 'Successfully mocked POST!'}),
          postResponse.data);
    });

    // Alternatively you can use onRoute chain to pass custom requests
    test('Expects Dioadapter to mock the data with onRoute', () async {
      final dio = Dio();
      final dioAdapter = DioAdapter();

      dio.httpClientAdapter = dioAdapter;
      dioAdapter
          .onRoute(path, request: Request(method: RequestMethods.PATCH))
          .reply(200, {
            'message': 'Successfully mocked PATCH!'
          }) // only use double quotes
          .onRoute(path, request: Request(method: RequestMethods.DELETE))
          .reply(200, {'message': 'Successfully mocked DELETE!'});

      // Making dio.get request on the path an expecting mocked response
      final patchResponse = await dio.patch(path);
      expect(jsonEncode({'message': 'Successfully mocked PATCH!'}),
          patchResponse.data);

      // Making dio.post request on the path an expecting mocked response
      final deleteResposne = await dio.delete(path);
      expect(jsonEncode({'message': 'Successfully mocked DELETE!'}),
          deleteResposne.data);
    });
  });

  // Also, for mocking requests, you can use dio Interceptor
  group('DioInterceptor usage', () {
    // Creating dio instance for mocking.
    // For instance: you can use your own instance from injection and add
    // DioInterceptor in dio.interceptors list
    final dioForInterceptor = Dio();
    final dioInterceptor =
        DioInterceptor(); // creating DioInterceptor instance for mocking requests

    dioForInterceptor.interceptors.add(dioInterceptor);

    const path = 'https://example2.com';

    test('Expects Dioadapter to mock the data', () async {
      // Defining request types and their responses respectively with their paths
      dioInterceptor
          .onDelete(path)
          .reply(200,
              {'message': 'Successfully mocked GET!'}) // only use double quotes
          .onPatch(path)
          .reply(200, {'message': 'Successfully mocked POST!'});

      // Making dio.delete request on the path an expecting mocked response
      final getResponse = await dioForInterceptor.delete(path);
      expect(jsonEncode({'message': 'Successfully mocked GET!'}),
          getResponse.data);

      // Making dio.patch request on the path an expecting mocked response
      final postResposne = await dioForInterceptor.patch(path);
      expect(jsonEncode({'message': 'Successfully mocked POST!'}),
          postResposne.data);
    });
  });

  group('Raising the custrom Error onRequest', () {
    const path = 'https://example.com';

    test('Test that throws raises custom exception', () async {
      final dio = Dio();
      final dioAdapter = DioAdapter();

      dio.httpClientAdapter = dioAdapter;
      const type = DioErrorType.RESPONSE;
      final response = Response(statusCode: 500);
      const error = 'Some beautiful error';

      // Building request to throw the DioError exception
      // on onGet for the specific path
      dioAdapter.onGet(path).throws(
            500,
            DioError(
              type: type,
              response: response,
              error: error,
            ),
          );

      // Checking that exception type can match `AdapterError` type too
      expect(() async => await dio.get(path),
          throwsA(TypeMatcher<AdapterError>()));

      // Checking that exception type can match `DioError` type too
      expect(() async => await dio.get(path), throwsA(TypeMatcher<DioError>()));

      // Checking the type and the message of the exception
      expect(
          () async => await dio.get(path),
          throwsA(
              predicate((DioError e) => e is DioError && e.message == error)));
    });
  });
}
154
likes
0
pub points
97%
popularity

Publisher

verified publisherlomsa.com

HTTP mock adapter for Dio & Mockito. By simply defining requests and corresponding responses through predefined adapters, you will be able to mock requests sent via Dio.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

dio, meta, mockito

More

Packages that depend on http_mock_adapter