httpmock | Lightweight HTTP mocking in Go | Mock library
kandi X-RAY | httpmock Summary
kandi X-RAY | httpmock Summary
This library builds on Go's built-in httptest library, adding a more mockable interface that can be used easily with other mocking tools like testify/mock. It does this by providing a Handler that receives HTTP components as separate arguments rather than a single *http.Request object.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- JSONMatcher returns a matcher that matches the given arguments
- NewUnstartedServer returns a new Server instance
- MultiHeaderMatcher returns a matcher that matches the provided HTTP headers .
- ToJSON marshals an object to JSON
- NewServer creates and starts a new Server
- HeaderMatcher creates a new HTTP header matcher
- URL returns the server s URL
httpmock Key Features
httpmock Examples and Code Snippets
s := httpmock.NewServer(&httpmock.OKHandler{})
defer s.Close()
// Make any requests you want to s.URL(), using it as the mock downstream server
downstream := &httpmock.MockHandler{}
// A simple GET that returns some pre-canned content
down
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
// Handler is the interface used by httpmock instead of http.Handler so that it can be mocked very easily.
type Handler interface {
Handle(method, path string, body []byte) Response
}
Community Discussions
Trending Discussions on httpmock
QUESTION
ANSWER
Answered 2021-Apr-09 at 16:52I am thinking because of the delay
, your assertion inside of your subscribe
never runs.
Try this to ensure that the assertions inside of the subscribe are running
QUESTION
I'm working on getting my HttpInterceptor to pass my unit tests and I have run into issues with getting the tests to pass. I have 6 tests and If I run the tests as "normal" tests, I often get "An error was thrown in afterAll" as the tests finish locally, but my Azure pipelines fail with the same error every time.
If I run the tests in an async zone using fakeAsync()
, I get a "1 timer(s) still in the queue" error for each.
Here's the code for the Interceptor:
...ANSWER
Answered 2021-Mar-12 at 09:42So, after some research on the HttpTestingController
I was able to recreate and reproduce the error. The solution is not far away from my previous answer, but it not was correct at all.
There is a tick(200)
missing after the subscribe()
, but as you are returning an error, you have to handle the error in the subscription.
So here you have a working example of the first test.
QUESTION
I struggeling to finish a unit test for my HttpInterceptor. The interceptor works as a global error handler and simply triggers on catchError(httpResponseError). The interceptor is working just fine on my site, but I can't get the unit test to actually test the code.
Here's the code for the Interceptor:
...ANSWER
Answered 2021-Mar-08 at 18:15In my opinion your problem comes from 'retry(1)'.
You have to call numberOfRetry + 1
times your api if you want to pass in catchError
function.
Maybe you can try that :
QUESTION
I am writing Jest for the components to make sure that handleError will be executed.
The UserService is defined as:
...ANSWER
Answered 2021-Feb-02 at 10:04Your return type for the actual http request mock is wrong
jest.spyOn(httpMock, 'post').mockReturnValue(of(throwError(error)));
Change it to
jest.spyOn(httpMock, 'post').mockReturnValue(throwError(error));
and your expected assertion should pop up.
QUESTION
I am struggling with unit testing my interceptor since yesterday noon. After following several tutorial and SO answers, I have a code that so far looks the best. BUT, because of some reason I am getting an error:
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL) Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL) at Jasmine
EDIT: I just added Stackblitz example.
my test:
...ANSWER
Answered 2021-Jan-12 at 14:54I took the time to read your interceptor and turns out your implemention is wrong, you should return an observeble:
QUESTION
once more I'm trying to write a test for a http get request and it fails because of datatyping (at least that's what I suspect).
The request I want to test works and looks like that
...ANSWER
Answered 2021-Jan-05 at 07:55mockType
is an object, whereas getTypeFilter
iterates the response for (const item of data)
.
Therefore reqMock.flush
should use an array too.
Try reqMock.flush([mockType]);
about types issues, it could be fixed like that:
QUESTION
I have created a UserService which saves a user to the database. I also have a service which pre-configures the endpoints, for local , qa, uat and prod called UrlService. This class returns in a promise the URls using APP_INITIALIZER, which works fine. It seems the configuration in the assets folder urls.json is interfering with the tests. When I added a console.log in the matches method, it prints out the path in /asset/urls.json. I don't understand how this could be interfering with my tests. I am perplexed this would have any bearing on my tests, but any insight will help.
When I run my tests it fails with the error:
...ANSWER
Answered 2020-Dec-01 at 04:35The problem is that httpMock
is triggered twice, so when you expectOne
it fails and throws the error message you showed: ... found 2 requests.
.
To solve this you can define the expected request, something similar to:
QUESTION
I am trying to unit test a getter and I get the error Expected 4 arguments, but got 1.
when trying to use said getter. I used quasar to build out the app and I am using typescript.
HttpMocks.mockToken
is just a generic JWT string
ANSWER
Answered 2020-Nov-30 at 10:40Problem was just I wasn't following the getter's signature.
QUESTION
I'm moving my first steps in Angular testing. In particular, I am trying to test this function in LoginComponent
ANSWER
Answered 2020-Oct-14 at 14:46This should be handled using Mock
rather than making actual http call by calling the service.
QUESTION
I have the following function I'm trying to unit test:
...ANSWER
Answered 2020-Oct-11 at 17:44I think calling the function should put two HTTP calls in queue. Try flushing both calls in series and putting your assertions in the .then
block.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install httpmock
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page