CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
MIT License
134 stars 67 forks source link

Is there any way to mock http response? #26

Closed taiju59 closed 4 years ago

taiju59 commented 4 years ago

Hello, I want to mock http response.

Like MockClient. (in http package)

import 'package:http/http.dart';
import 'package:http/testing.dart';

final MockClient mockClient = MockClient((request) async {
  if (request.url.path == '/foo') {
    return Response('foo', 200);
  }
  return Response('bar', 200);
}

But _client instance in HttpClientWithInterceptor is final, private and initialized in field, so it's not injectable.

Then, I tried to mock with InterceptorContract.

But interceptRequest can't modify response. And interceptResponse can't stop request, so (real) request thrown. I want to mock without connecting to the internet.

Is there any way to mock http response?

(If I can't, I request feature to inject client instance.)

Sorry for my poor English, Thanks.

issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.82. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

CodingAleCR commented 4 years ago

Hi, no worries about your English, it's perfect. And as to your question, right now there's no way to mock it, I am currently working on making the library better and compatible with the much needed testing we all want. If you want I can let you know when I've added it via email or GitHub as well. Thanks for the comments and suggestions.

taiju59 commented 4 years ago

@CodingAleCR Thank you for quick reply! OK, I understand. So I want to request feature to inject client instance. If it might be good, I will create PR.
Should I create new issue?

CodingAleCR commented 4 years ago

I will add it to the roadmap since I keep it separately from the repository in GitHub. I will however link back the PR to this issue once it's ready. Thanks! 🎉