Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,42 @@
public class BlockResponse {

/**
* HTTP status code.
* HTTP status code used for the actual HTTP response.
*/
private int status;

/**
* Business code carried in the response body, independent of {@link #status}.
*/
private int code;

private String message;
private String route;

/**
* @deprecated use {@link #BlockResponse(int, int, String, String)} instead.
* The given {@code code} will be used as both the HTTP status and the body code.
*/
@Deprecated
public BlockResponse(int code, String message, String route) {
this(code, code, message, route);
}

public BlockResponse(int status, int code, String message, String route) {
this.status = status;
this.code = code;
this.message = message;
this.route = route;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public int getCode() {
return code;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Object run() throws ZuulException {

// Set fallback response.
ctx.setResponseBody(blockResponse.toString());
ctx.setResponseStatusCode(blockResponse.getCode());
ctx.setResponseStatusCode(blockResponse.getStatus());
// Set Response ContentType
ctx.getResponse().setContentType("application/json; charset=utf-8");
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback;

import org.junit.Assert;
import org.junit.Test;

/**
* Test cases for {@link BlockResponse}.
*/
public class BlockResponseTest {

@Test
public void testDeprecatedConstructorKeepsStatusAndCodeInSync() {
BlockResponse response = new BlockResponse(429, "blocked", "/foo");

Assert.assertEquals(429, response.getStatus());
Assert.assertEquals(429, response.getCode());
}

@Test
public void testStatusAndCodeCanBeCustomizedIndependently() {
// Body code can differ from the actual HTTP status, see issue #2985.
BlockResponse response = new BlockResponse(200, 10018, "blocked", "/foo");

Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(10018, response.getCode());
Assert.assertTrue(response.toString().contains("\"code\":10018"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,42 @@
public class BlockResponse {

/**
* HTTP status code.
* HTTP status code used for the actual HTTP response.
*/
private int status;

/**
* Business code carried in the response body, independent of {@link #status}.
*/
private int code;

private String message;
private String route;

/**
* @deprecated use {@link #BlockResponse(int, int, String, String)} instead.
* The given {@code code} will be used as both the HTTP status and the body code.
*/
@Deprecated
public BlockResponse(int code, String message, String route) {
this(code, code, message, route);
}

public BlockResponse(int status, int code, String message, String route) {
this.status = status;
this.code = code;
this.message = message;
this.route = route;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public int getCode() {
return code;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public HttpResponseMessage apply(HttpRequestMessage request) {
ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager
.getFallbackProvider(fallBackRoute);
BlockResponse response = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, throwable);
HttpResponseMessage resp = new HttpResponseMessageImpl(context, request, response.getCode());
HttpResponseMessage resp = new HttpResponseMessageImpl(context, request, response.getStatus());
resp.setBodyAsText(response.toString());
return resp;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback;

import org.junit.Assert;
import org.junit.Test;

/**
* Test cases for {@link BlockResponse}.
*/
public class BlockResponseTest {

@Test
public void testDeprecatedConstructorKeepsStatusAndCodeInSync() {
BlockResponse response = new BlockResponse(429, "blocked", "/foo");

Assert.assertEquals(429, response.getStatus());
Assert.assertEquals(429, response.getCode());
}

@Test
public void testStatusAndCodeCanBeCustomizedIndependently() {
// Body code can differ from the actual HTTP status, see issue #2985.
BlockResponse response = new BlockResponse(200, 10018, "blocked", "/foo");

Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(10018, response.getCode());
Assert.assertTrue(response.toString().contains("\"code\":10018"));
}
}