Coder Social home page Coder Social logo

Multiple requests about dart_nock HOT 12 CLOSED

cah4a avatar cah4a commented on September 15, 2024
Multiple requests

from dart_nock.

Comments (12)

cah4a avatar cah4a commented on September 15, 2024 1

Hey @jerem17, I've got a solution for you.

Try adding TestWidgetsFlutterBinding.ensureInitialized(); in setUpAll hook function.

Cookies won't work anyway, because it saved to SharedPreferences which doesn't exist at test time.
If you rely on them, you might open an issue there.

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

Hi!

Sorry for the late reply.

Seems passing strings instead of matchers is working as expected.
I mean DioError [DioErrorType.RESPONSE]: Http status error [404] is arising.

I was rushing forward while implementing URL matching, maybe should do some fixes to match the expected behavior.

Thanks for the issue. That helps a lot.

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

Hey @pblinux!

I've updated the package recently to support that use case. Just use version 1.1.2.
Thanks for your contribution!

from dart_nock.

jerem17 avatar jerem17 commented on September 15, 2024

Hello @cah4a ,
first of all thank you for the great work!
On the other hand, I have the same problem, I do 2 nock (url) .get ("/ example1) and another nock (url) .get (" / example2). The first goes well, but the second timeout ... I put "persist" to try but that doesn't change anything ...

 nock("https://example.com/1").get("")
        ..reply(
          200,
          "ahaha",
        );

   nock("https://example.com/2").get("")
        ..reply(
          200,
          "ohoho",
        );

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

Hey @jerem17,

Thanks for the kind words!

Just tried sequential scenario and it seems working:

import 'package:test/test.dart';
import 'package:nock/nock.dart';
import 'package:http/http.dart' as http;

void main() {
  setUpAll(() {
    nock.init();
    nock.defaultBase = "http://example.com";
  });

  setUp(() {
    nock.cleanAll();
  });

  test('sequntal_requests', () async {
    final interceptor = nock.get("/users")
      ..reply(
        200,
        "result1",
      );

    final interceptor2 = nock.get("/users")
      ..reply(
        200,
        "result2",
      );

    final request1 = await http.get("http://example.com/users");
    final request2 = await http.get("http://example.com/users");

    expect(request1.body, "result1");
    expect(request2.body, "result2");

    expect(interceptor.isDone, true);
    expect(interceptor2.isDone, true);
  });
name: sequential_scenario_dart_nock_test

dev_dependencies:
  test: ^1.15.7
  nock: ^1.1.2
$ dart --version
Dart SDK version: 2.10.4 (stable) (Wed Nov 11 13:35:58 2020 +0100) on "macos_x64"

If that doesn't help feel free to open a new issue, with more detailed information.

from dart_nock.

jerem17 avatar jerem17 commented on September 15, 2024

@cah4a Thank you for your quick feedback! This changes nothing, I think the problem is that I am calling an external function. It looks like this:

my test.dart :

 test('test1', () async {
    
      final interceptor = nock.get("/users1")
        ..reply(
          200,
          "result1",
        );

      final interceptor2 = nock.get("/users2")
        ..reply(
          200,
          "result2",
        );


      expect(() async => auth.signIn("us1", "us2"), throwsException);
    }, timeout: Timeout(Duration(seconds: 10)));

my external function, which is in a class I called "Auth" and which uses the "requests" package for http requests;

class Auth {
signIn(String us1, String us2) {
 await Requests.get("http://example.com/users1", headers: {});
    print("step 1");

    await Requests.get("http://example.com/users2", headers: {});
    print("step 2" );
    }
}

The first request is to intercept, but not the second: /

Dart SDK version: 2.12.0-133.2.beta (beta) (Tue Dec 15 09:55:09 2020 +0100) on "macos_x64"

dev_dependencies:
test: ^1.15.7
nock: ^1.1.2

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

Should your code throw an exception?

import 'package:test/test.dart';
import 'package:nock/nock.dart';
import 'package:http/http.dart' as http;

Future<void> extern_function() async {
    await http.get("http://example.com/users1");
    await http.get("http://example.com/users2");
}

void main() {
  setUpAll(() {
    nock.init();
    nock.defaultBase = "http://example.com";
  });

  setUp(() {
    nock.cleanAll();
  });

  test('sequntal_requests', () async {
    nock.get("/users1")
      ..reply(
        200,
        "result1",
      );

    nock.get("/users2")
      ..reply(
        200,
        "result2",
      );
      
    // TEST NOT PASS:
    // expect(() async => extern_function(), throwsException);
      
    // TEST PASS:
    expect(extern_function(), completes);
  });
}

from dart_nock.

jerem17 avatar jerem17 commented on September 15, 2024

@cah4a So I got some good news for you and some bad news for me lol

When I test your code using the http package, it works perfectly, however, when I use the requests package, it doesn't work anymore.

func :

Future<void> extern_function() async {
  print("1");
  await Requests.get("http://example.com/users1");
  print("2");
  await Requests.get("http://example.com/users2");
  print("3");
}

result :

1
2
TimeoutException after 0:00:10.000000: Test timed out after 10 seconds.
dart:isolate  _RawReceivePortImpl._handleMessage

So I guess I have to look at the "request" package side to find the solution to my problem

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

Hmm, that's pretty interesting.
Not that good news, btw. Nock has to support any library out there.

from dart_nock.

jerem17 avatar jerem17 commented on September 15, 2024

in addition I use the requests package in all my application lol
rip

Anyway thank you very much for taking the time to answer me and help me

from dart_nock.

jerem17 avatar jerem17 commented on September 15, 2024

You are a genius! Thank you very much, it works now.

from dart_nock.

cah4a avatar cah4a commented on September 15, 2024

@jerem17
My pleasure!

from dart_nock.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.