GithubHelp home page GithubHelp logo

Negative matching about wiremock HOT 3 CLOSED

tobyweston avatar tobyweston commented on August 19, 2024
Negative matching

from wiremock.

Comments (3)

tomakehurst avatar tomakehurst commented on August 19, 2024

Hi,

It's not possible to do that quite as simply you've stated, but you could achieve the same thing by pulling down the logged requests and inspecting them e.g.

List<LoggedRequest> requests = findAll(getRequestedFor(urlEqualTo("/test")));
assertThat(requests, hasItem(withoutHeader("Some-Header", "some-value")));

where withoutHeader() returns a Hamcrest matcher over HttpHeader values.

Hope that's some help.

I've got a bit of a backlog of stuff I want to do at the moment, but I'll try and get your suggestion added at some point.

Tom

from wiremock.

tobyweston avatar tobyweston commented on August 19, 2024

Great, thanks.

I've gone with something like this to tide me over...

@Test
public void headerIsNotSet() throws MalformedURLException {
    // make http call
    verifyNoHeadersFor(urlEqualTo("/test"));
}

private void verifyNoHeadersFor(UrlMatchingStrategy url) {
    List<LoggedRequest> requests = findAll(getRequestedFor(url));
    assertThat(requests, not(contains(HeaderMatcher.header("Authorization"))));
}

private static class HeaderMatcher extends TypeSafeMatcher<LoggedRequest> {
    private final String header;

    private HeaderMatcher(String header) {
        this.header = header;
    }

    public static HeaderMatcher header(String header) {
        return new HeaderMatcher(header);
    }

    @Override
    protected boolean matchesSafely(LoggedRequest actual) {
        return actual.containsHeader(header);
    }

    @Override
    public void describeTo(Description description) {
        description.appendValue(header);
    }
}

from wiremock.

tomakehurst avatar tomakehurst commented on August 19, 2024

I've added withoutHeader() now. Please let me know if you use it and have any issues with it.

Cheers,
Tom

from wiremock.

Related Issues (20)

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.