Best Go-testdeep code snippet using td.TestContext
testUtil.go
Source:testUtil.go
...33/**34 Function called after create/get policy test35 Modify the ID in outputBody and expected body36**/37func PostCreateGetPolicyTest(data interface{}, context *TestContext) {38 if restTD, ok := data.(*RestTestData); ok {39 postCreateGetPolicyTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)40 } else if cmdTD, ok := data.(*CmdTestData); ok {41 postCreateGetPolicyTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)42 } else if grpcTD, ok := data.(*GRpcTestData); ok {43 postCreateGetPolicyTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)44 } else {45 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")46 }47}48func postCreateGetPolicyTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {49 if outputBody != nil {50 //check if outputPolicy is pms.Policy (used in kauctl command and REST test)51 outputPolicy, ok := outputBody.(*pms.Policy)52 if ok {53 context.NameIDMap[outputPolicy.Name] = outputPolicy.ID54 context.NameObjectMap[outputPolicy.Name] = outputPolicy55 } else {56 TestLog.Log("Fail to convert outputBody to policy object array")57 return58 }59 } else {60 return61 }62 if expectedBody != nil {63 expectedPolicy, ok := expectedBody.(*pms.Policy)64 if ok {65 expectedPolicy.ID = context.NameIDMap[expectedPolicy.Name]66 }67 }68}69func postCreateGetPolicyTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {70 if outputBody != nil {71 outputPolicy, ok := outputBody.(*pb.Policy)72 if ok {73 context.NameIDMap[outputPolicy.Name] = outputPolicy.Id74 } else {75 TestLog.Log("Fail to convert outputBody to policy object array")76 return77 }78 } else {79 return80 }81 if expectedBody != nil {82 expectedPolicy, ok := expectedBody.(*pb.Policy)83 if ok {84 expectedPolicy.Id = context.NameIDMap[expectedPolicy.Name]85 }86 }87}88/**89 Function called after List policy test90 Modify the ID in outputBody and expectedBody91**/92func PostListPolicyTest(data interface{}, context *TestContext) {93 if restTD, ok := data.(*RestTestData); ok {94 postListPolicyTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)95 } else if cmdTD, ok := data.(*CmdTestData); ok {96 postListPolicyTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)97 } else if grpcTD, ok := data.(*GRpcTestData); ok {98 postListPolicyTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)99 } else {100 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")101 }102}103func postListPolicyTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {104 adjustMap := make(map[string]*pms.Policy)105 if outputBody != nil {106 outputPolicy, ok := outputBody.(*[]*pms.Policy)107 if !ok {108 TestLog.Log("Fail to convert outputBody to policy object array")109 return110 }111 for i := 0; i < len(*outputPolicy); i = i + 1 {112 context.NameIDMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i].ID113 adjustMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i]114 }115 } else {116 return117 }118 newOutputBody := []*pms.Policy{}119 if expectedBody != nil {120 expectedPolicy, ok := expectedBody.(*[]*pms.Policy)121 if !ok {122 TestLog.Log("Fail to convert expectedBody to policy object")123 return124 }125 for i := 0; i < len(*expectedPolicy); i = i + 1 {126 id, ok := context.NameIDMap[(*expectedPolicy)[i].Name]127 if ok {128 (*expectedPolicy)[i].ID = id129 }130 exp, ok := adjustMap[(*expectedPolicy)[i].Name]131 if ok {132 newOutputBody = append(newOutputBody, exp)133 delete(adjustMap, (*expectedPolicy)[i].Name)134 }135 }136 }137 for _, value := range adjustMap {138 newOutputBody = append(newOutputBody, value)139 }140 outputPolicy, ok := outputBody.(*[]*pms.Policy)141 if !ok {142 TestLog.Log("Fail to convert outputBody to policy object array")143 return144 }145 *outputPolicy = newOutputBody146}147func postListPolicyTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {148 adjustMap := make(map[string]*pb.Policy)149 if outputBody != nil {150 outputPolicy, ok := outputBody.(*[]*pb.Policy)151 if !ok {152 TestLog.Log("Fail to convert outputBody to pb.policy object array")153 return154 }155 for i := 0; i < len(*outputPolicy); i = i + 1 {156 context.NameIDMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i].Id157 adjustMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i]158 }159 } else {160 return161 }162 newOutputBody := []*pb.Policy{}163 if expectedBody != nil {164 expectedPolicy, ok := expectedBody.(*[]*pb.Policy)165 if !ok {166 TestLog.Log("Fail to convert expectedBody to pb.policy object")167 return168 }169 for i := 0; i < len(*expectedPolicy); i = i + 1 {170 id, ok := context.NameIDMap[(*expectedPolicy)[i].Name]171 if ok {172 (*expectedPolicy)[i].Id = id173 }174 exp, ok := adjustMap[(*expectedPolicy)[i].Name]175 if ok {176 newOutputBody = append(newOutputBody, exp)177 delete(adjustMap, (*expectedPolicy)[i].Name)178 }179 }180 }181 for _, value := range adjustMap {182 newOutputBody = append(newOutputBody, value)183 }184 outputPolicy, ok := outputBody.(*[]*pb.Policy)185 if !ok {186 TestLog.Log("Fail to convert outputBody to pb.policy object array")187 return188 }189 *outputPolicy = newOutputBody190}191/**192 Function called after create/get role policy test193 Modify the ID in outputBody and expected body194**/195func PostCreateGetRolePolicyTest(data interface{}, context *TestContext) {196 if restTD, ok := data.(*RestTestData); ok {197 postCreateGetRolePolicyTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)198 } else if cmdTD, ok := data.(*CmdTestData); ok {199 postCreateGetRolePolicyTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)200 } else if grpcTD, ok := data.(*GRpcTestData); ok {201 postCreateGetRolePolicyTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)202 } else {203 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")204 }205}206func postCreateGetRolePolicyTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {207 var outputPolicy *pms.RolePolicy208 if outputBody != nil {209 var ok bool210 outputPolicy, ok = outputBody.(*pms.RolePolicy)211 if !ok {212 return213 }214 context.NameIDMap[outputPolicy.Name] = outputPolicy.ID215 context.NameObjectMap[outputPolicy.Name] = outputPolicy216 } else {217 return218 }219 if expectedBody != nil {220 expectedPolicy, ok := expectedBody.(*pms.RolePolicy)221 if !ok {222 return223 }224 expectedPolicy.ID = outputPolicy.ID225 }226}227func postCreateGetRolePolicyTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {228 var outputPolicy *pb.RolePolicy229 if outputBody != nil {230 ok := false231 outputPolicy, ok = outputBody.(*pb.RolePolicy)232 if !ok {233 return234 }235 context.NameIDMap[outputPolicy.Name] = outputPolicy.Id236 } else {237 return238 }239 if expectedBody != nil {240 expectedPolicy, ok := expectedBody.(*pb.RolePolicy)241 if !ok {242 return243 }244 expectedPolicy.Id = context.NameIDMap[expectedPolicy.Name]245 }246}247/**248 Function called after List role policy test249 Modify the ID in outputBody and expected body250**/251func PostListRolePolicyTest(data interface{}, context *TestContext) {252 if restTD, ok := data.(*RestTestData); ok {253 postListRolePolicyTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)254 } else if cmdTD, ok := data.(*CmdTestData); ok {255 postListRolePolicyTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)256 } else if grpcTD, ok := data.(*GRpcTestData); ok {257 postListRolePolicyTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)258 } else {259 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")260 }261}262func postListRolePolicyTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {263 adjustMap := make(map[string]*pms.RolePolicy)264 if outputBody != nil {265 outputPolicy, ok := outputBody.(*[]*pms.RolePolicy)266 if !ok {267 return268 }269 for i := 0; i < len(*outputPolicy); i = i + 1 {270 context.NameIDMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i].ID271 adjustMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i]272 }273 } else {274 return275 }276 newOutputBody := []*pms.RolePolicy{}277 if expectedBody != nil {278 expectedPolicy, ok := expectedBody.(*[]*pms.RolePolicy)279 if !ok {280 return281 }282 for i := 0; i < len(*expectedPolicy); i = i + 1 {283 id, ok := context.NameIDMap[(*expectedPolicy)[i].Name]284 if ok {285 (*expectedPolicy)[i].ID = id286 }287 exp, ok := adjustMap[(*expectedPolicy)[i].Name]288 if ok {289 newOutputBody = append(newOutputBody, exp)290 delete(adjustMap, (*expectedPolicy)[i].Name)291 }292 }293 }294 for _, value := range adjustMap {295 newOutputBody = append(newOutputBody, value)296 }297 outputPolicy, ok := outputBody.(*[]*pms.RolePolicy)298 if !ok {299 return300 }301 *outputPolicy = newOutputBody302}303func postListRolePolicyTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {304 adjustMap := make(map[string]*pb.RolePolicy)305 if outputBody != nil {306 outputPolicy, ok := outputBody.(*[]*pb.RolePolicy)307 if !ok {308 return309 }310 for i := 0; i < len(*outputPolicy); i = i + 1 {311 context.NameIDMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i].Id312 adjustMap[(*outputPolicy)[i].Name] = (*outputPolicy)[i]313 }314 } else {315 return316 }317 newOutputBody := []*pb.RolePolicy{}318 if expectedBody != nil {319 expectedPolicy, ok := expectedBody.(*[]*pb.RolePolicy)320 if !ok {321 return322 }323 for i := 0; i < len(*expectedPolicy); i = i + 1 {324 id, ok := context.NameIDMap[(*expectedPolicy)[i].Name]325 if ok {326 (*expectedPolicy)[i].Id = id327 }328 exp, ok := adjustMap[(*expectedPolicy)[i].Name]329 if ok {330 newOutputBody = append(newOutputBody, exp)331 delete(adjustMap, (*expectedPolicy)[i].Name)332 }333 }334 }335 for _, value := range adjustMap {336 newOutputBody = append(newOutputBody, value)337 }338 outputPolicy, ok := outputBody.(*[]*pb.RolePolicy)339 if !ok {340 return341 }342 *outputPolicy = newOutputBody343}344/**345 Function called after Create or Get service test346 Modify the ID in outputBody and expected body347**/348func PostCreateGetServiceTest(data interface{}, context *TestContext) {349 if restTD, ok := data.(*RestTestData); ok {350 postCreateGetServiceTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)351 } else if cmdTD, ok := data.(*CmdTestData); ok {352 postCreateGetServiceTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)353 } else if grpcTD, ok := data.(*GRpcTestData); ok {354 postCreateGetServiceTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)355 } else {356 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")357 }358}359func postCreateGetServiceTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {360 if outputBody != nil {361 outputService, ok := outputBody.(*pms.Service)362 if !ok {363 TestLog.Log("Fail to convert outputBody to pms.Service")364 return365 }366 expectService, ok := expectedBody.(*pms.Service)367 //The policies/rolepolicies is nil in expect/output sometimes368 if outputService.Policies == nil {369 TestLog.Log("outputService.Policies is nil. Set it to Empty")370 outputService.Policies = []*pms.Policy{}371 }372 if outputService.RolePolicies == nil {373 TestLog.Log("outputService.RolePolicies is nil. Set it to Empty")374 outputService.RolePolicies = []*pms.RolePolicy{}375 }376 if expectService.Policies == nil {377 TestLog.Log("expectService.Policies is nil. Set it to Empty")378 expectService.Policies = []*pms.Policy{}379 }380 if expectService.RolePolicies == nil {381 TestLog.Log("expectService.RolePolicies is nil. Set it to Empty")382 expectService.RolePolicies = []*pms.RolePolicy{}383 }384 postListPolicyTest_rest(&outputService.Policies, &expectService.Policies, context)385 postListRolePolicyTest_rest(&outputService.RolePolicies, &expectService.RolePolicies, context)386 } else {387 return388 }389}390func postCreateGetServiceTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {391 if outputBody != nil {392 outputService, ok := outputBody.(*pb.Service)393 if !ok {394 TestLog.Log("Fail to convert outputBody to pb.Service")395 return396 }397 expectService, ok := expectedBody.(*pb.Service)398 postListPolicyTest_rest(&outputService.Policies, &expectService.Policies, context)399 postListRolePolicyTest_rest(&outputService.RolePolicies, &expectService.RolePolicies, context)400 } else {401 return402 }403}404/**405 Function called after listing service test406 Modify the ID in outputBody and expected body407**/408func PostListServiceTest(data interface{}, context *TestContext) {409 if restTD, ok := data.(*RestTestData); ok {410 postListServiceTest_rest(restTD.OutputBody, restTD.ExpectedBody, context)411 } else if cmdTD, ok := data.(*CmdTestData); ok {412 postListServiceTest_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)413 } else if grpcTD, ok := data.(*GRpcTestData); ok {414 postListServiceTest_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)415 } else {416 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")417 }418}419func postListServiceTest_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {420 if outputBody != nil {421 adjustMap := make(map[string]*pms.Service)422 outputService, ok := outputBody.(*[]*pms.Service)423 if !ok {424 TestLog.Log("Fail to convert outputBody to pms.Service")425 return426 }427 for i := 0; i < len(*outputService); i = i + 1 {428 adjustMap[(*outputService)[i].Name] = (*outputService)[i]429 }430 newOutputBody := []*pms.Service{}431 if expectedBody != nil {432 expectedService, ok := expectedBody.(*[]*pms.Service)433 if !ok {434 return435 }436 for i := 0; i < len(*expectedService); i = i + 1 {437 exp, ok := adjustMap[(*expectedService)[i].Name]438 if ok {439 newOutputBody = append(newOutputBody, exp)440 delete(adjustMap, (*expectedService)[i].Name)441 }442 }443 }444 for _, value := range adjustMap {445 newOutputBody = append(newOutputBody, value)446 }447 *outputService = newOutputBody448 } else {449 return450 }451}452func postListServiceTest_grpc(outputBody interface{}, expectedBody interface{}, context *TestContext) {453 if outputBody != nil {454 adjustMap := make(map[string]*pb.Service)455 outputService, ok := outputBody.(*[]*pb.Service)456 if !ok {457 TestLog.Log("Fail to convert outputBody to pb.Service")458 return459 }460 for i := 0; i < len(*outputService); i = i + 1 {461 adjustMap[(*outputService)[i].Name] = (*outputService)[i]462 }463 newOutputBody := []*pb.Service{}464 if expectedBody != nil {465 expectedService, ok := expectedBody.(*[]*pb.Service)466 if !ok {467 return468 }469 for i := 0; i < len(*expectedService); i = i + 1 {470 exp, ok := adjustMap[(*expectedService)[i].Name]471 if ok {472 newOutputBody = append(newOutputBody, exp)473 delete(adjustMap, (*expectedService)[i].Name)474 }475 }476 }477 for _, value := range adjustMap {478 newOutputBody = append(newOutputBody, value)479 }480 *outputService = newOutputBody481 } else {482 return483 }484}485//Sort the output by role name string486func PostGetAllGrantedRoles(data interface{}, context *TestContext) {487 if restTD, ok := data.(*RestTestData); ok {488 postGetAllGrantedRoles_rest(restTD.OutputBody, restTD.ExpectedBody, context)489 } else if cmdTD, ok := data.(*CmdTestData); ok {490 postGetAllGrantedRoles_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)491 //} else if grpcTD, ok := data.(*GRpcTestData); ok {492 //postGetAllGrantedRoles_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)493 } else {494 TestLog.Fatalf("Only RestTestData, CmdTestData, are supported by now!")495 }496}497func postGetAllGrantedRoles_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {498 if outputBody != nil {499 outputRoles, _ := outputBody.(*[]string)500 sort.Strings(*outputRoles)501 }502}503//Sort the output by resource name string504func PostGetAllGrantedPermissions(data interface{}, context *TestContext) {505 if restTD, ok := data.(*RestTestData); ok {506 postGetAllGrantedPermissions_rest(restTD.OutputBody, restTD.ExpectedBody, context)507 } else if cmdTD, ok := data.(*CmdTestData); ok {508 postGetAllGrantedPermissions_rest(cmdTD.OutputBody, cmdTD.ExpectedBody, context)509 //} else if grpcTD, ok := data.(*GRpcTestData); ok {510 //postGetAllGrantedPermissions_grpc(grpcTD.OutputBody, grpcTD.ExpectedBody, context)511 } else {512 TestLog.Fatalf("Only RestTestData, CmdTestData, are supported by now!")513 }514}515func postGetAllGrantedPermissions_rest(outputBody interface{}, expectedBody interface{}, context *TestContext) {516 if outputBody != nil {517 adjustMap := make(map[string]pms.Permission)518 outputPerms, ok := outputBody.(*[]pms.Permission)519 if !ok {520 return521 }522 for i := 0; i < len(*outputPerms); i = i + 1 {523 adjustMap[(*outputPerms)[i].Resource] = (*outputPerms)[i]524 }525 res := make([]string, len(*outputPerms))526 j := 0527 for k, _ := range adjustMap {528 res[j] = k529 j++530 }531 sort.Strings(res)532 newOutputBody := []pms.Permission{}533 for _, v := range adjustMap {534 newOutputBody = append(newOutputBody, v)535 }536 *outputPerms = newOutputBody537 } else {538 return539 }540}541//Function called before Get/Delete policy/rolepolicy542//Update the policy/rolepolicy name with ID in the request543// since we only support get/delete with ID544func PreGetDeletePolicyTest(data interface{}, context *TestContext) {545 if _, ok := data.(*RestTestData); ok {546 preGetDeletePolicy_rest(data, context)547 //} else if cmdTD, ok := data.(*CmdTestData); ok {548 // preGetDeletePolicy_cmd(data, context)549 } else if _, ok := data.(*GRpcTestData); ok {550 preGetDeletePolicy_grpc(data, context)551 } else {552 TestLog.Fatalf("Only RestTestData, CmdTestData, GRpcTestData are supported by now!")553 }554}555//update policy/rolepolicy's name with ID in URI556//the uri should be like http://127.0.0.1:6733/policy-mgmt/v1/service/srv1/policy/policy1557//update policy1 with its's ID558func preGetDeletePolicy_rest(data interface{}, context *TestContext) {559 restTD, _ := data.(*RestTestData)560 pos := strings.LastIndex(restTD.URI, "/")561 policyName := restTD.URI[pos+1 : len(restTD.URI)]562 id, ok := context.NameIDMap[policyName]563 if ok {564 restTD.URI = restTD.URI[0:pos+1] + id565 }566}567//Set policyID in grpc request. Name is specified in request.PolicyID or request.RolePolicyID568//And we should update it with ID of policy/rolePolicy569func preGetDeletePolicy_grpc(data interface{}, context *TestContext) {570 grpcTD := data.(*GRpcTestData)571 request, ok := grpcTD.InputBody.(*pb.PolicyQueryRequest)572 if ok {573 id, ok1 := context.NameIDMap[request.PolicyID]574 if ok1 {575 request.PolicyID = id576 }577 } else {578 TestLog.Log("preGetDeletePolicy_grpc----2")579 request, ok := grpcTD.InputBody.(*pb.RolePolicyQueryRequest)580 if ok {581 TestLog.Log("preGetDeletePolicy_grpc----2-RolePolicy")582 id, ok1 := context.NameIDMap[request.RolePolicyID]583 if ok1 {...
api_create_test.go
Source:api_create_test.go
...14func TestCreate(t *testing.T) {15 suite.Run(t, new(createSuite))16}17func (suite *createSuite) BeforeTest(suiteName, testName string) {18 TestContext.BeforeTest()19}20func (suite *createSuite) TestCreateWithNewShortUrlReturns201() {21 t := suite.T()22 testServer := TestContext.server23 testAPI := tdhttp.NewTestAPI(t, testServer)24 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "https://www.cloudflare.com"}).25 CmpStatus(http.StatusCreated).26 CmpJSONBody(27 td.JSON(28 `{29 "short_url": "$shortUrl",30 "slug": "$slug",31 "long_url": "$longUrl",32 "expires_on": "$expiresOn",33 "created_at": "$createdAt"34 }`,35 td.Tag("shortUrl", td.Re("http:\\/\\/example\\.com\\/([A-Za-z0-9]{8})")),36 td.Tag("slug", td.Re("[A-Za-z0-9]{8}")),37 td.Tag("longUrl", "https://www.cloudflare.com"),38 td.Tag("expiresOn", td.Nil()),39 td.Tag("createdAt", td.Smuggle(parseDateTime, td.Between(testAPI.SentAt(), time.Now()))),40 ),41 )42}43func (suite *createSuite) TestCreateWithInvalidLongUrlReturns400() {44 t := suite.T()45 testServer := TestContext.server46 testAPI := tdhttp.NewTestAPI(t, testServer)47 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "invalid"}).48 CmpStatus(http.StatusBadRequest).49 CmpJSONBody(50 td.JSON(51 `{52 "errors": [53 {54 "field": "LongUrl",55 "reason": "url"56 }57 ],58 }`,59 ),60 )61}62func (suite *createSuite) TestCreateWithInvalidSchemeReturns400() {63 t := suite.T()64 testServer := TestContext.server65 testAPI := tdhttp.NewTestAPI(t, testServer)66 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "javascript:alert('hi')"}).67 CmpStatus(http.StatusBadRequest).68 CmpJSONBody(69 td.JSON(70 `{71 "errors": [72 {73 "field": "LongUrl",74 "reason": "only http and https are supported"75 }76 ],77 }`,78 ),79 )80}81func (suite *createSuite) TestCreateWithExpirationDateReturns201() {82 t := suite.T()83 testServer := TestContext.server84 testAPI := tdhttp.NewTestAPI(t, testServer)85 expirationDateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)86 testAPI.PostJSON(87 "/api/v1/shorturls",88 gin.H{89 "long_url": "https://www.cloudflare.com",90 "expires_on": expirationDateTime.Format(time.RFC3339),91 },92 ).93 CmpStatus(http.StatusCreated).94 CmpJSONBody(95 td.JSON(96 `{97 "short_url": "$shortUrl",98 "slug": "$slug",99 "long_url": "$longUrl",100 "expires_on": "$expiresOn",101 "created_at": "$createdAt"102 }`,103 td.Tag("shortUrl", td.Re("http:\\/\\/example\\.com\\/([A-Za-z0-9]{8})")),104 td.Tag("slug", td.Re("[A-Za-z0-9]{8}")),105 td.Tag("longUrl", "https://www.cloudflare.com"),106 td.Tag("expiresOn", td.Smuggle(parseDateTime, expirationDateTime)),107 td.Tag("createdAt", td.Smuggle(parseDateTime, td.Between(testAPI.SentAt(), time.Now()))),108 ),109 )110}111func (suite *createSuite) TestCreateWithExistingLongUrlReturns200() {112 t := suite.T()113 testServer := TestContext.server114 testAPI := tdhttp.NewTestAPI(t, testServer)115 var slug string116 var shortUrl string117 var longUrl string118 var createdAt time.Time119 // Initial POST should return a 201 CREATED120 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "https://www.cloudflare.com"}).121 CmpStatus(http.StatusCreated).122 CmpJSONBody(123 td.JSON(124 `{125 "short_url": "$shortUrl",126 "slug": "$slug",127 "long_url": "$longUrl",128 "expires_on": "$expiresOn",129 "created_at": "$createdAt"130 }`,131 td.Tag("slug", td.Catch(&slug, td.Re("[A-Za-z0-9]{8}"))),132 td.Tag("shortUrl", td.Catch(&shortUrl, td.Ignore())),133 td.Tag("longUrl", td.Catch(&longUrl, "https://www.cloudflare.com")),134 td.Tag("expiresOn", td.Nil()),135 td.Tag("createdAt", td.Smuggle(parseDateTime, td.Catch(&createdAt, td.Ignore()))),136 ),137 )138 // Second POST should return a 200 OK with information about existing long URL139 testAPI.PostJSON("/api/v1/shorturls", gin.H{"long_url": "https://www.cloudflare.com"}).140 CmpStatus(http.StatusOK).141 CmpJSONBody(142 td.JSON(143 `{144 "short_url": "$shortUrl",145 "slug": "$slug",146 "long_url": "$longUrl",147 "expires_on": "$expiresOn",148 "created_at": "$createdAt"149 }`,150 td.Tag("shortUrl", shortUrl),151 td.Tag("slug", slug),152 td.Tag("longUrl", longUrl),153 td.Tag("expiresOn", td.Nil()),154 td.Tag("createdAt", td.Smuggle(parseDateTime, createdAt)),155 ),156 )157}158func (suite *createSuite) TestCreateWithExistingSlugReturns409() {159 t := suite.T()160 testServer := TestContext.server161 testAPI := tdhttp.NewTestAPI(t, testServer)162 slug := "cf"163 testAPI.PostJSON(164 "/api/v1/shorturls",165 gin.H{166 "slug": slug,167 "long_url": "https://www.cloudflare.com",168 },169 ).170 CmpStatus(http.StatusCreated).171 CmpJSONBody(172 td.SuperJSONOf(`{"slug": "$slug"}`, td.Tag("slug", slug)),173 )174 testAPI.PostJSON(175 "/api/v1/shorturls",176 gin.H{177 "long_url": "https://www.stackoverflow.com",178 "slug": slug,179 },180 ).181 CmpStatus(http.StatusConflict).182 CmpJSONBody(183 td.JSON(184 `{185 "errors": [186 {187 "field": "Slug",188 "reason": "must be unique"189 }190 ],191 }`,192 ),193 )194}195func (suite *createSuite) TestCreateWithInvalidJSONReturns400() {196 t := suite.T()197 testServer := TestContext.server198 testAPI := tdhttp.NewTestAPI(t, testServer)199 testAPI.PostJSON("/api/v1/shorturls", gin.H{"foo": "bar"}).200 CmpStatus(http.StatusBadRequest).201 CmpJSONBody(202 td.JSON(203 `{204 "errors": [205 {206 "field": "LongUrl",207 "reason": "required",208 }209 ]210 }`,211 td.Tag("slug", "cf"),...
TestContext
Using AI Code Generation
1import (2func main() {3 fmt.Println("main() started")4 fmt.Println("Calling test()")5 test()6 fmt.Println("main() stopped")7}8func test() {9 fmt.Println("test() started")10 ctx := td.TestContext()11 fmt.Println("test() stopped")12}13main() started14Calling test()15test() started16test() stopped17main() stopped18import (19func main() {20 fmt.Println("main() started")21 fmt.Println("Calling test()")22 test()23 fmt.Println("main() stopped")24}25func test() {26 fmt.Println("test() started")27 ctx := td.TestContext()28 fmt.Println("test() stopped")29}30main() started31Calling test()32test() started33test() stopped34main() stopped35import (36func main() {37 fmt.Println("main() started")38 fmt.Println("Calling test()")39 test()40 fmt.Println("main() stopped")41}42func test() {43 fmt.Println("test() started")44 ctx := td.TestContext()45 fmt.Println("test() stopped")46}47main() started48Calling test()49test() started50test() stopped51main() stopped52import (53func main() {54 fmt.Println("main() started")55 fmt.Println("Calling test()")56 test()57 fmt.Println("main() stopped")58}59func test() {60 fmt.Println("test() started")61 ctx := td.TestContext()62 fmt.Println("test() stopped")63}64main() started65Calling test()66test() started67test() stopped68main() stopped69import (70func main() {71 fmt.Println("main() started")
TestContext
Using AI Code Generation
1import (2func TestContext(t *testing.T) {3 t.Log("TestContext")4}5import (6func Test(t *testing.T) {7 t.Log("Test")8}9import (10func TestContext(t *testing.T) {11 t.Log("TestContext")12}13import (14func TestContext(t *testing.T) {15 t.Log("TestContext")16}17import (18func TestContext(t *testing.T) {19 t.Log("TestContext")20}21import (22func TestContext(t *testing.T) {23 t.Log("TestContext")24}25import (26func TestContext(t *testing.T) {27 t.Log("TestContext")28}29import (30func TestContext(t *testing.T) {31 t.Log("TestContext")32}33import (34func TestContext(t *testing.T) {35 t.Log("TestContext")36}37import (38func TestContext(t *testing.T) {39 t.Log("TestContext")40}41import (42func TestContext(t *testing.T) {43 t.Log("TestContext")44}45import (
TestContext
Using AI Code Generation
1import (2func TestContext(t *testing.T) {3 fmt.Println("TestContext")4}5func main() {6 fmt.Println("main")7}8import (9func TestMain(t *testing.M) {10 fmt.Println("TestMain")11}12func main() {13 fmt.Println("main")14}15import (16func Test(t *testing.T) {17 fmt.Println("Test")18}19func main() {20 fmt.Println("main")21}22import (23func TestXxx(t *testing.T) {24 fmt.Println("TestXxx")25}26func main() {27 fmt.Println("main")28}
TestContext
Using AI Code Generation
1import (2func TestContext(t *testing.T) {3 t.Log("TestContext")4}5func main() {6 fmt.Println("TestContext")7}8--- PASS: TestContext (0.00s)
TestContext
Using AI Code Generation
1import "testing"2func TestContext(t *testing.T) {3 t.Log("hello")4 t.Log("world")5 t.Log("!")6}7func TestNotContext(t *testing.T) {8 t.Log("hello")9 t.Log("world")10 t.Log("!")11}12import "testing"13func TestContext(t *testing.T) {14 t.Log("hello")15 t.Log("world")16 t.Log("!")17}18func TestNotContext(t *testing.T) {19 t.Log("hello")20 t.Log("world")21 t.Log("!")22}23import "testing"24func TestContext(t *testing.T) {25 t.Log("hello")26 t.Log("world")27 t.Log("!")28}29func TestNotContext(t *testing.T) {30 t.Log("hello")31 t.Log("world")32 t.Log("!")33}34import "testing"35func TestContext(t *testing.T) {36 t.Log("hello")37 t.Log("world")38 t.Log("!")39}40func TestNotContext(t *testing.T) {41 t.Log("hello")42 t.Log("world")43 t.Log("!")44}45import "testing"46func TestContext(t *testing.T) {47 t.Log("hello")48 t.Log("world")49 t.Log("!")50}51func TestNotContext(t *testing.T) {52 t.Log("hello")53 t.Log("world")54 t.Log("!")55}56import "testing"57func TestContext(t *testing.T) {58 t.Log("hello")59 t.Log("world")60 t.Log("!")61}62func TestNotContext(t *testing.T) {63 t.Log("hello")64 t.Log("world")65 t.Log("!")66}67import "testing"68func TestContext(t *testing.T) {
TestContext
Using AI Code Generation
1import (2type TestStruct struct {3}4func (ts *TestStruct) TestContext() {5 ts.t.Log("TestContext called")6}7func TestMain(t *testing.T) {8 ts := &TestStruct{t: t}9 ts.TestContext()10}11import (12type TestStruct struct {13}14func (ts *TestStruct) TestContext() {15 ts.t.Log("TestContext called")16}17func TestMain(t *testing.T) {18 ts := &TestStruct{t: t}19 ts.TestContext()20}21import (22type TestStruct struct {23}24func (ts *TestStruct) TestContext() {25 ts.t.Log("TestContext called")26}27func TestMain(t *testing.T) {28 ts := &TestStruct{t: t}29 ts.TestContext()30}31import (32type TestStruct struct {33}34func (ts *TestStruct) TestContext() {35 ts.t.Log("TestContext called")36}37func TestMain(t *testing.T) {38 ts := &TestStruct{t: t}39 ts.TestContext()40}41import (42type TestStruct struct {43}44func (ts *TestStruct) TestContext() {45 ts.t.Log("TestContext called")46}47func TestMain(t *testing.T) {48 ts := &TestStruct{t: t}49 ts.TestContext()50}51import (52type TestStruct struct {
TestContext
Using AI Code Generation
1import (2func main() {3 tc := td.NewTestContext()4 t := tc.NewTest("Test1", "This is a test", "This is a test", "This is a test")5 t.Run()6 fmt.Println(t.GetResults())7}8import (9func main() {10 tc := td.NewTestContext()11 t := tc.NewTest("Test1", "This is a test", "This is a test", "This is a test")12 t.Run()13 fmt.Println(t.GetResults())14}15import (16func main() {17 tc := td.NewTestContext()18 t := tc.NewTest("Test1", "This is a test", "This is a test", "This is a test")19 t.Run()20 fmt.Println(t.GetResults())21}22import (23func main() {24 tc := td.NewTestContext()25 t := tc.NewTest("Test1", "This is a test", "This is a test", "This is a test")26 t.Run()27 fmt.Println(t.GetResults())28}29import (30func main() {31 tc := td.NewTestContext()32 t := tc.NewTest("Test1", "This
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!