question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding headers when using MockMvcTarget

See original GitHub issue

When using SpringRestPactRunner and MockMvcTarget the @TargetRequestFilter is never being invoked so I am not able to add headers. Is there another way? It looks like @TargetRequestFilter only works when using HttpTarget

@RunWith(SpringRestPactRunner.class)
@Provider("provider-name")
@PactBroker
public class PactVerificationTest {

    @Mock
    private MockedService mockedService;

    @InjectMocks
    private MyController controller = new MyController(mockedService);

    @TestTarget
    public final MockMvcTarget target = new MockMvcTarget();

    @Before
    public void before() {
        //initialize your mocks using your mocking framework
        MockitoAnnotations.initMocks(this);

        //configure the MockMvcTarget with your controller and controller advice
        target.setControllers(controller);
        target.setPrintRequestResponse(true);
    }

    // This block is never invoked
    @TargetRequestFilter
    public void requestFilter(HttpRequest request) {
        request.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
    }

    @State({"person"})
    public void verify() throws DownstreamServiceException {
        target.setRunTimes(1);
        // ... mockito mocks
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
uglyogcommented, Jan 26, 2020

Version 4.0.5 has been released with this change

1reaction
uglyogcommented, Nov 24, 2019

I’ve added support for request filters, but the parameter will be an instance of MockHttpServletRequestBuilder not HttpRequest

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding headers when using MockMvcTarget · Issue #1339
I am getting below error while passing headers in @TargetRequestFilter public void requestFilter(MockHttpServletRequestBuilder request) ...
Read more >
Developers - Adding headers when using MockMvcTarget -
I am getting below error while passing headers in @TargetRequestFilter public void requestFilter(MockHttpServletRequestBuilder request) {
Read more >
How to add a header to a MockMvc request, depending on ...
How to post-process the request builder to automatically apply .header("Authorization", "Bearer foo") if the test is decorated with @WithJwt ?
Read more >
Changelog | Pact Docs
da6efdec9 - GH-1632 invent default auth header with additional tests ... add support for request filters with MockMvcTarget #983 (Ronald ...
Read more >
Add a Header to a Jersey SSE Client Request
In this tutorial, we'll see an easy way to send headers in Server-Sent Event (SSE) client requests using the Jersey Client API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found