Test Webhook where there is an old doc revision and where the filter function is expecting an old doc revision.
(t *testing.T)
| 623 | // Test Webhook where there is an old doc revision and where the filter |
| 624 | // function is expecting an old doc revision. |
| 625 | func TestWebhookOldDoc(t *testing.T) { |
| 626 | terminator := make(chan bool) |
| 627 | defer close(terminator) |
| 628 | |
| 629 | ts, wr := InitWebhookTest() |
| 630 | defer ts.Close() |
| 631 | url := ts.URL |
| 632 | |
| 633 | ids := make([]string, 200) |
| 634 | for i := 0; i < 200; i++ { |
| 635 | ids[i] = fmt.Sprintf("%d", i) |
| 636 | } |
| 637 | |
| 638 | eventForTest := func(k string, v int) (Body, string, base.Set) { |
| 639 | testBody := Body{ |
| 640 | BodyId: ids[v], |
| 641 | "value": k, |
| 642 | } |
| 643 | var channelSet base.Set |
| 644 | if v%2 == 0 { |
| 645 | channelSet = base.SetFromArray([]string{"Even"}) |
| 646 | } else { |
| 647 | channelSet = base.SetFromArray([]string{"Odd"}) |
| 648 | } |
| 649 | return testBody, ids[v], channelSet |
| 650 | } |
| 651 | |
| 652 | // Test basic webhook where an old doc is passed but not filtered |
| 653 | log.Println("Test basic webhook where an old doc is passed but not filtered") |
| 654 | em := NewEventManager(terminator) |
| 655 | ctx := base.TestCtx(t) |
| 656 | em.Start(ctx, 0, -1) |
| 657 | webhookHandler, _ := NewWebhook(ctx, fmt.Sprintf("%s/echo", url), "", nil, nil) |
| 658 | em.RegisterEventHandler(ctx, webhookHandler, DocumentChange) |
| 659 | for i := 0; i < 10; i++ { |
| 660 | oldBody, oldDocId, _ := eventForTest(strconv.Itoa(-i), i) |
| 661 | oldBody[BodyId] = oldDocId |
| 662 | oldBodyBytes := base.MustJSONMarshal(t, oldBody) |
| 663 | body, docId, channels := eventForTest(strconv.Itoa(i), i) |
| 664 | bodyBytes := base.MustJSONMarshal(t, body) |
| 665 | err := em.RaiseDocumentChangeEvent(ctx, bodyBytes, docId, string(oldBodyBytes), channels, false) |
| 666 | assert.NoError(t, err) |
| 667 | |
| 668 | } |
| 669 | err := em.waitForProcessedTotal(ctx, 10, DefaultWaitForWebhook) |
| 670 | assert.NoError(t, err) |
| 671 | assert.Equal(t, int64(10), em.eventsProcessedSuccess) |
| 672 | log.Printf("Actual: %v, Expected: %v", wr.GetCount(), 10) |
| 673 | |
| 674 | // Test webhook where an old doc is passed and is not used by the filter |
| 675 | log.Println("Test filter function with old doc which is not referenced") |
| 676 | wr.Clear() |
| 677 | em = NewEventManager(terminator) |
| 678 | em.Start(ctx, 0, -1) |
| 679 | filterFunction := `function(doc) { |
| 680 | if (doc.value < 6) { |
| 681 | return false; |
| 682 | } else { |
nothing calls this directly
no test coverage detected