dreamhead / moco

Easy Setup Stub Server
MIT License
4.35k stars 1.08k forks source link

How to enable basic authentication with moco http(s)? #155

Open yaravind opened 8 years ago

yaravind commented 8 years ago

I need to stub basic authentication support. I am wondering how to test it?

dreamhead commented 8 years ago

Could you please give me more details?

yaravind commented 8 years ago

My real REST service forces a HTTP(S) Basic authentication (username/password) for all of its clients. I'd like to stub this behavior using MOCO while testing my clients. Reference: https://www.httpwatch.com/httpgallery/authentication/

yaravind commented 8 years ago

@dreamhead can you please comment?

dreamhead commented 8 years ago

Why not put HTTP header directly?

yaravind commented 8 years ago

I can add the header from client perspective. I am wondering how to fail the test if wrong credentials are passed?

dreamhead commented 8 years ago

400 will be returned if no credential is matched. You can also write a configuration for wrong credentials if you need a specific response.

yaravind commented 8 years ago

Thanks. I got this working with HTTP. However, with HTTP(S) the following test is (in Groovy using Spock)

def "Supports Jenkins with SSL and Basic Authentication"() {
        given: "Correct username and password"

        //trust the self-signed cert.jks
        setTrustStore("/cert.jks", "mocohttps")

        def httpsServer = httpsServer(10443, HttpsCertificate.certificate(pathResource("cert.jks"), "mocohttps", "mocohttps"))
        httpsServer.request(and(by(uri("/api/json")), by(header("Authorization", "Basic dXNlcjpwYXNzd29yZA=="))))
                .response(pathResource("single-job.json"))
        mock = runner(httpsServer)
        mock.start()

        def userName = "user"
        def password = "password"

        when:
        def url = new URL("https://localhost:10443/api/json")
        def jenkins = new JenkinsClient(url, userName, password)
        def response = jenkins.get()

        then: "A valid response should be present"
        response.isPresent() == true

        mock.stop()
    }

the following error

groovy.lang.MissingMethodException: No signature of method: com.github.dreamhead.moco.action.MocoRequestAction.call() is applicable for argument types: (com.github.dreamhead.moco.handler.HeaderResponseHandler) values: [com.github.dreamhead.moco.handler.HeaderResponseHandler@4dd6fd0a]
Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), each(groovy.lang.Closure), find()

    at org.aravind.oss.jenkins.JenkinsClientTest.Supports Jenkins with SSL and Basic Authentication(JenkinsClientTest.groovy:152)

I believe it has something to do with telling the httpsServer to expect the "Authorization" header.

dreamhead commented 8 years ago

It seems you are using MocoRequestAction which should not be used here. But I'm not quite sure which line is the root cause.

Misterhex commented 6 years ago

facing the same issue here.