在放心 post 调用中获取不受支持的字符集 \"ISO-8859-1\ 错误
Getting unsupported charset \"ISO-8859-1\ error in rest assured post call
我试图使用 Best Buy api playground 进行 post 调用,我编写了常用代码来进行 post 调用,但我收到了 500 错误字符集不受支持“ISO-8859-1。我正在使用数据提供程序 class 将 json 有效负载传递给 post 调用
这是 swagger API 文档
这里是控制台日志:-
2021-06-18 11:48:15 INFO BestBuy Rest API:29 - ----Create new product request test started----
2021-06-18 11:48:16 INFO BestBuy Rest API:57 - The response for create product
{
"name": "GeneralError",
"message": "unsupported charset \"ISO-8859-1\"",
"code": 500,
"className": "general-error",
"data": {
},
"errors": {
}
}
2021-06-18 11:48:16 INFO BestBuy Rest API:36 - ----Create new product request test completed
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
这里是 post 调用的代码
public class TC002_BestBuy_CreateProduct extends TestBase{
private static String prodID;
public static String getVariable(){
return prodID;
}
public static void setVariable(String var){
prodID = var;
}
@BeforeClass
public void createProductData(){
logger.info("----Create new product request test started----");
TestBase.initialization();
}
@AfterClass
public void tearDown(){
logger.info("----Create new product request test completed");
}
@Test(priority = 1,dataProvider = "apiDataProvider",dataProviderClass = APIDataProvider.class,description = "post call and status check for create product")
public void createProduct(String name,String type,String price,String shipping,String upc,String description,String manufacturer,String model,String url,String image){
TestBase.response= given()
.baseUri(property.getProperty("BaseURI"))
.queryParam("name",name)
.queryParam("type",type)
.queryParam("price",price)
.queryParam("shipping",shipping)
.queryParam("upc",upc)
.queryParam("description",description)
.queryParam("manufacturer",manufacturer)
.queryParam("model",model)
.queryParam("url",url)
.queryParam("image",image)
.post(property.getProperty("createProductURL"));
logger.info("The response for create product");
TestBase.response.prettyPrint();
String productId=TestBase.response.jsonPath().get("id");
setVariable(productId);
}
}
这是 excel 实用程序 class 读取 excel 数据的代码
public static int getRowCount(String xlfile,String xlsheet) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
int rowcount=ws.getLastRowNum();
wb.close();
fi.close();
return rowcount;
}
public static int getCellCount(String xlfile,String xlsheet,int rownum) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
int cellcount=row.getLastCellNum();
wb.close();
fi.close();
return cellcount;
}
public static String getcellData(String sheetname, int rowNum, int colNum) {
row = ws.getRow(rowNum);
cell = row.getCell(colNum);
int index = wb.getSheetIndex(sheetname);
if (rowNum <= 0) {
return "";
} else if (index == -1) {
return "";
} else if (cell == null) {
return "";
} else if (row == null) {
return "";
} else if (cell.getCellTypeEnum() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA) {
String cellValue = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {
DateFormat df = new SimpleDateFormat("dd/mm/yy");
Date date = cell.getDateCellValue();
cellValue = df.format(date);
}
return cellValue;
} else if (cell.getCellTypeEnum() == CellType.BLANK) {
return "";
} else return String.valueOf(cell.getBooleanCellValue());
}
public static void setCellData(String xlfile,String xlsheet,int rownum,int colnum,String data) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
cell=row.createCell(colnum);
cell.setCellValue(data);
fo=new FileOutputStream(xlfile);
wb.write(fo);
wb.close();
fi.close();
fo.close();
}
}
这里是数据提供者class,从excel
读取后提供数据
public class APIDataProvider {
@DataProvider(name = "apiDataProvider")
public Object[][] getEmpData() throws IOException {
String path=System.getProperty("user.dir")+"/TestResources/TestData.xlsx";
int row=ExcelUtils.getRowCount(path,"Sheet1");
int col=ExcelUtils.getCellCount(path,"Sheet1",1);
String empdata[][]=new String[row][col];
for (int i = 1; i <= row; i++) {
for (int j = 0; j < col; j++) {
empdata[i - 1][j] = ExcelUtils.getcellData("Sheet1", i, j);
}
}
return (empdata);
}
}
我大摇大摆地看到请求是 POST,而不是 Get,因此您应该更改如何设置 API 调用:更改 queryParam --> post 正文。
阅读更多:https://github.com/rest-assured/rest-assured/wiki/Usage#request-body。
示例:
@Test
void demo() {
BestBuyProduct aProduct = new BestBuyProduct();
aProduct.setName("apple");
aProduct.setType("fruit");
aProduct.setPrice(30);
aProduct.setShipping(30);
aProduct.setUpc("DF400");
aProduct.setDescription("test");
aProduct.setManufacturer("XYZ");
aProduct.setModel("AB123");
aProduct.setUrl("http://example");
aProduct.setImage("/apple.img");
given().contentType(ContentType.JSON)
.body(aProduct)
.post("to_your_api_endpoint");
}
要从 POJO 转换 --> json,不要忘记将 jackson 或 gson 添加到类路径。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
顺便说一句,不是直接使用 Apache POI,而是可以使用 https://github.com/ozlerhakan/poiji 转换 excel --> POJO。
这将有助于减少读取 excel 文件的复杂性,并减少测试用例的参数数量。
我试图使用 Best Buy api playground 进行 post 调用,我编写了常用代码来进行 post 调用,但我收到了 500 错误字符集不受支持“ISO-8859-1。我正在使用数据提供程序 class 将 json 有效负载传递给 post 调用
这是 swagger API 文档
这里是控制台日志:-
2021-06-18 11:48:15 INFO BestBuy Rest API:29 - ----Create new product request test started----
2021-06-18 11:48:16 INFO BestBuy Rest API:57 - The response for create product
{
"name": "GeneralError",
"message": "unsupported charset \"ISO-8859-1\"",
"code": 500,
"className": "general-error",
"data": {
},
"errors": {
}
}
2021-06-18 11:48:16 INFO BestBuy Rest API:36 - ----Create new product request test completed
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
这里是 post 调用的代码
public class TC002_BestBuy_CreateProduct extends TestBase{
private static String prodID;
public static String getVariable(){
return prodID;
}
public static void setVariable(String var){
prodID = var;
}
@BeforeClass
public void createProductData(){
logger.info("----Create new product request test started----");
TestBase.initialization();
}
@AfterClass
public void tearDown(){
logger.info("----Create new product request test completed");
}
@Test(priority = 1,dataProvider = "apiDataProvider",dataProviderClass = APIDataProvider.class,description = "post call and status check for create product")
public void createProduct(String name,String type,String price,String shipping,String upc,String description,String manufacturer,String model,String url,String image){
TestBase.response= given()
.baseUri(property.getProperty("BaseURI"))
.queryParam("name",name)
.queryParam("type",type)
.queryParam("price",price)
.queryParam("shipping",shipping)
.queryParam("upc",upc)
.queryParam("description",description)
.queryParam("manufacturer",manufacturer)
.queryParam("model",model)
.queryParam("url",url)
.queryParam("image",image)
.post(property.getProperty("createProductURL"));
logger.info("The response for create product");
TestBase.response.prettyPrint();
String productId=TestBase.response.jsonPath().get("id");
setVariable(productId);
}
}
这是 excel 实用程序 class 读取 excel 数据的代码
public static int getRowCount(String xlfile,String xlsheet) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
int rowcount=ws.getLastRowNum();
wb.close();
fi.close();
return rowcount;
}
public static int getCellCount(String xlfile,String xlsheet,int rownum) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
int cellcount=row.getLastCellNum();
wb.close();
fi.close();
return cellcount;
}
public static String getcellData(String sheetname, int rowNum, int colNum) {
row = ws.getRow(rowNum);
cell = row.getCell(colNum);
int index = wb.getSheetIndex(sheetname);
if (rowNum <= 0) {
return "";
} else if (index == -1) {
return "";
} else if (cell == null) {
return "";
} else if (row == null) {
return "";
} else if (cell.getCellTypeEnum() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA) {
String cellValue = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {
DateFormat df = new SimpleDateFormat("dd/mm/yy");
Date date = cell.getDateCellValue();
cellValue = df.format(date);
}
return cellValue;
} else if (cell.getCellTypeEnum() == CellType.BLANK) {
return "";
} else return String.valueOf(cell.getBooleanCellValue());
}
public static void setCellData(String xlfile,String xlsheet,int rownum,int colnum,String data) throws IOException
{
fi=new FileInputStream(xlfile);
wb=new XSSFWorkbook(fi);
ws=wb.getSheet(xlsheet);
row=ws.getRow(rownum);
cell=row.createCell(colnum);
cell.setCellValue(data);
fo=new FileOutputStream(xlfile);
wb.write(fo);
wb.close();
fi.close();
fo.close();
}
}
这里是数据提供者class,从excel
读取后提供数据public class APIDataProvider {
@DataProvider(name = "apiDataProvider")
public Object[][] getEmpData() throws IOException {
String path=System.getProperty("user.dir")+"/TestResources/TestData.xlsx";
int row=ExcelUtils.getRowCount(path,"Sheet1");
int col=ExcelUtils.getCellCount(path,"Sheet1",1);
String empdata[][]=new String[row][col];
for (int i = 1; i <= row; i++) {
for (int j = 0; j < col; j++) {
empdata[i - 1][j] = ExcelUtils.getcellData("Sheet1", i, j);
}
}
return (empdata);
}
}
我大摇大摆地看到请求是 POST,而不是 Get,因此您应该更改如何设置 API 调用:更改 queryParam --> post 正文。 阅读更多:https://github.com/rest-assured/rest-assured/wiki/Usage#request-body。 示例:
@Test
void demo() {
BestBuyProduct aProduct = new BestBuyProduct();
aProduct.setName("apple");
aProduct.setType("fruit");
aProduct.setPrice(30);
aProduct.setShipping(30);
aProduct.setUpc("DF400");
aProduct.setDescription("test");
aProduct.setManufacturer("XYZ");
aProduct.setModel("AB123");
aProduct.setUrl("http://example");
aProduct.setImage("/apple.img");
given().contentType(ContentType.JSON)
.body(aProduct)
.post("to_your_api_endpoint");
}
要从 POJO 转换 --> json,不要忘记将 jackson 或 gson 添加到类路径。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
顺便说一句,不是直接使用 Apache POI,而是可以使用 https://github.com/ozlerhakan/poiji 转换 excel --> POJO。 这将有助于减少读取 excel 文件的复杂性,并减少测试用例的参数数量。