Skip to content

MockHttpServletRequest setCookies should join cookies to single Cookie header #23029

@chas678

Description

@chas678

Version

spring-test-5.1.6.RELEASE

Overview

setCookies on a MockHttpServletRequest sets multiple "Cookie" Headers. It should concatenate the cookie name/value pairs into a single "Cookie" header string joined with delimiter "; ".

MockHttpServletRequest setCookies javadoc has little detail on what behavior a user should expect. If the behavior deviates from rfc6265 syntax by design then could the documentation be amended to reflect that?

Test case exposing defect

    @Test
    public void cookiesRegressionIssue() {
        // potentially regression of https://github.com/spring-projects/spring-framework/issues/19790 ?
        // ** arrange
        MockHttpServletRequest request = new MockHttpServletRequest();
        Cookie cookie1 = new Cookie("foo", "bar");
        Cookie cookie2 = new Cookie("baz", "qux");

        // ** act
        // Javadoc gives no detail:
        // https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mock/web/MockHttpServletRequest.html#setCookies-javax.servlet.http.Cookie...-
        request.setCookies(cookie1, cookie2);

        // ** assert
        Cookie[] cookies = request.getCookies();
        assertAll("Cookies>Headers conversion should work",
                () -> assertThat(cookies.length, is(2)),
                () -> assertThat(cookies[0].getName(), is("foo")),
                () -> assertThat(cookies[0].getValue(), is("bar")),
                () -> assertThat(cookies[1].getName(), is("baz")),
                () -> assertThat(cookies[1].getValue(), is("qux")),
                () -> assertThat(request.getHeader("Cookie"), is("foo=bar; baz=qux")), 
                () -> assertThat(Collections.list(request.getHeaders("Cookie")), hasSize(1))
        );
    }

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)status: feedback-providedFeedback has been providedtype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions