-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(http): fix java.lang.IllegalStateException: STREAMED #5367
Merged
halibobo1205
merged 13 commits into
tronprotocol:develop
from
forfreeday:feat/fix_servlet
Aug 18, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
029c040
feat(Servlet): Optimize Http Servlet
forfreeday 9003b0e
feat(util): optimization parameter check
0bcaaad
feat(Servlet): Optimize request parameter fetching
0664c4a
test(Servlet): modify assertion conditions
e404b18
feat(Util): Optimize of the method into the parameter
b1a2870
feat(Util): simplified return logic
8bad304
feat(Util): remove Content-type checksums
b3400df
feat(Util): fix codeStyle
75d832e
test(case): add routine use cases
304dba4
test(case): add json case
c8a0cc5
test(case): fix case
9cc6b5e
test(case): remove useless methods
1d3036a
Merge remote-tracking branch 'upstream/develop' into feat/fix_servlet
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For historical reasons, this logic does not have a unit test. Better add comprehensive unit tests for all situations after changing the whole logic of the param extraction to make sure all the new logic works well, such as: good requests for both form&json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.tron.core.services.http; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import javax.annotation.Resource; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.tron.common.BaseTest; | ||
import org.tron.core.Constant; | ||
import org.tron.core.config.args.Args; | ||
|
||
public class GetBrokerageServletTest extends BaseTest { | ||
|
||
@Resource | ||
private GetBrokerageServlet getBrokerageServlet; | ||
|
||
static { | ||
dbPath = "db_GetBrokerageServlet_test"; | ||
Args.setParam( | ||
new String[]{ | ||
"--output-directory", dbPath, | ||
}, Constant.TEST_CONF | ||
); | ||
} | ||
|
||
public MockHttpServletRequest createRequest(String contentType) { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.setMethod("POST"); | ||
request.setContentType(contentType); | ||
request.setCharacterEncoding("UTF-8"); | ||
return request; | ||
} | ||
|
||
@Test | ||
public void getBrokerageValueByJsonTest() { | ||
int expect = 20; | ||
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; | ||
MockHttpServletRequest request = createRequest("application/json"); | ||
request.setContent(jsonParam.getBytes()); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
getBrokerageServlet.doPost(request, response); | ||
try { | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int brokerage = (int)result.get("brokerage"); | ||
Assert.assertEquals(expect, brokerage); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getBrokerageValueTest() { | ||
int expect = 20; | ||
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); | ||
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
getBrokerageServlet.doPost(request, response); | ||
try { | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int brokerage = (int)result.get("brokerage"); | ||
Assert.assertEquals(expect, brokerage); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getByBlankParamTest() { | ||
int expect = 0; | ||
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); | ||
request.addParameter("address", ""); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
getBrokerageServlet.doPost(request, response); | ||
try { | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int brokerage = (int)result.get("brokerage"); | ||
Assert.assertEquals(expect, brokerage); | ||
String content = (String) result.get("Error"); | ||
Assert.assertNull(content); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package org.tron.core.services.http; | ||
|
||
import static org.tron.common.utils.Commons.decodeFromBase58Check; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.UnsupportedEncodingException; | ||
import javax.annotation.Resource; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.FileUtil; | ||
import org.tron.core.Constant; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.db.Manager; | ||
import org.tron.core.service.MortgageService; | ||
import org.tron.core.store.DelegationStore; | ||
|
||
public class GetRewardServletTest extends BaseTest { | ||
|
||
@Resource | ||
private Manager manager; | ||
|
||
@Resource | ||
private MortgageService mortgageService; | ||
|
||
@Resource | ||
private DelegationStore delegationStore; | ||
|
||
@Resource | ||
GetRewardServlet getRewardServlet; | ||
|
||
static { | ||
dbPath = "db_GetRewardServlet_test"; | ||
Args.setParam( | ||
new String[]{ | ||
"--output-directory", dbPath, | ||
}, Constant.TEST_CONF | ||
); | ||
} | ||
|
||
public MockHttpServletRequest createRequest(String contentType) { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.setMethod("POST"); | ||
request.setContentType(contentType); | ||
request.setCharacterEncoding("UTF-8"); | ||
return request; | ||
} | ||
|
||
public void init() { | ||
manager.getDynamicPropertiesStore().saveChangeDelegation(1); | ||
byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); | ||
delegationStore.setBrokerage(0, sr, 10); | ||
delegationStore.setWitnessVote(0, sr, 100000000); | ||
} | ||
|
||
@Test | ||
public void getRewardValueByJsonTest() { | ||
init(); | ||
int expect = 138181; | ||
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; | ||
MockHttpServletRequest request = createRequest("application/json"); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
request.setContent(jsonParam.getBytes()); | ||
try { | ||
getRewardServlet.doPost(request, response); | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int reward = (int)result.get("reward"); | ||
Assert.assertEquals(expect, reward); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getRewardValueTest() { | ||
init(); | ||
int expect = 138181; | ||
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
mortgageService.payStandbyWitness(); | ||
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); | ||
getRewardServlet.doPost(request, response); | ||
try { | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int reward = (int)result.get("reward"); | ||
Assert.assertEquals(expect, reward); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getByBlankParamTest() { | ||
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
request.addParameter("address", ""); | ||
GetRewardServlet getRewardServlet = new GetRewardServlet(); | ||
getRewardServlet.doPost(request, response); | ||
try { | ||
String contentAsString = response.getContentAsString(); | ||
JSONObject result = JSONObject.parseObject(contentAsString); | ||
int reward = (int)result.get("reward"); | ||
Assert.assertEquals(0, reward); | ||
String content = (String) result.get("Error"); | ||
Assert.assertNull(content); | ||
} catch (UnsupportedEncodingException e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getParam
orcheckAndGetParam
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
checkGetParam
as the method name, because there are some checking judgments