| 1132 | } |
| 1133 | |
| 1134 | func TestBindApisErrors(t *testing.T) { |
| 1135 | vm := goja.New() |
| 1136 | BindApis(vm) |
| 1137 | |
| 1138 | scenarios := []struct { |
| 1139 | js string |
| 1140 | expectStatus int |
| 1141 | expectMessage string |
| 1142 | expectData string |
| 1143 | }{ |
| 1144 | {"new ApiError()", 0, "", "null"}, |
| 1145 | {"new ApiError(100, 'test', {'test': 1})", 100, "Test.", `{"test":1}`}, |
| 1146 | {"new NotFoundError()", 404, "The requested resource wasn't found.", "null"}, |
| 1147 | {"new NotFoundError('test', {'test': 1})", 404, "Test.", `{"test":1}`}, |
| 1148 | {"new BadRequestError()", 400, "Something went wrong while processing your request.", "null"}, |
| 1149 | {"new BadRequestError('test', {'test': 1})", 400, "Test.", `{"test":1}`}, |
| 1150 | {"new ForbiddenError()", 403, "You are not allowed to perform this request.", "null"}, |
| 1151 | {"new ForbiddenError('test', {'test': 1})", 403, "Test.", `{"test":1}`}, |
| 1152 | {"new UnauthorizedError()", 401, "Missing or invalid authentication.", "null"}, |
| 1153 | {"new UnauthorizedError('test', {'test': 1})", 401, "Test.", `{"test":1}`}, |
| 1154 | {"new TooManyRequestsError()", 429, "Too Many Requests.", "null"}, |
| 1155 | {"new TooManyRequestsError('test', {'test': 1})", 429, "Test.", `{"test":1}`}, |
| 1156 | {"new InternalServerError()", 500, "Something went wrong while processing your request.", "null"}, |
| 1157 | {"new InternalServerError('test', {'test': 1})", 500, "Test.", `{"test":1}`}, |
| 1158 | } |
| 1159 | |
| 1160 | for _, s := range scenarios { |
| 1161 | v, err := vm.RunString(s.js) |
| 1162 | if err != nil { |
| 1163 | t.Errorf("[%s] %v", s.js, err) |
| 1164 | continue |
| 1165 | } |
| 1166 | |
| 1167 | apiErr, ok := v.Export().(*router.ApiError) |
| 1168 | if !ok { |
| 1169 | t.Errorf("[%s] Expected ApiError, got %v", s.js, v) |
| 1170 | continue |
| 1171 | } |
| 1172 | |
| 1173 | if apiErr.Status != s.expectStatus { |
| 1174 | t.Errorf("[%s] Expected Status %d, got %d", s.js, s.expectStatus, apiErr.Status) |
| 1175 | } |
| 1176 | |
| 1177 | if apiErr.Message != s.expectMessage { |
| 1178 | t.Errorf("[%s] Expected Message %q, got %q", s.js, s.expectMessage, apiErr.Message) |
| 1179 | } |
| 1180 | |
| 1181 | dataRaw, _ := json.Marshal(apiErr.RawData()) |
| 1182 | if string(dataRaw) != s.expectData { |
| 1183 | t.Errorf("[%s] Expected Data %q, got %q", s.js, s.expectData, dataRaw) |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | func TestLoadingDynamicModel(t *testing.T) { |
| 1189 | app, _ := tests.NewTestApp() |