MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestProveAttachmentNotFound

Function TestProveAttachmentNotFound

rest/attachment_test.go:2460–2511  ·  view source on GitHub ↗

CBG-2004: Test that prove attachment over Blip works correctly when receiving a ErrAttachmentNotFound

(t *testing.T)

Source from the content-addressed store, hash-verified

2458
2459// CBG-2004: Test that prove attachment over Blip works correctly when receiving a ErrAttachmentNotFound
2460func TestProveAttachmentNotFound(t *testing.T) {
2461 rt := NewRestTester(t, &RestTesterConfig{PersistentConfig: true})
2462 defer rt.Close()
2463
2464 dbConfig := rt.NewDbConfig()
2465 RequireStatus(t, rt.CreateDatabase("db", dbConfig), http.StatusCreated)
2466 rt.GetDatabase().EnableAllowConflicts(rt.TB())
2467 rt.SetAdminParty(true)
2468
2469 bt := NewBlipTesterFromSpecWithRT(rt, nil)
2470 defer bt.Close()
2471
2472 attachmentData := []byte("attachmentA")
2473 attachmentDataEncoded := base64.StdEncoding.EncodeToString(attachmentData)
2474
2475 bt.blipContext.HandlerForProfile[db.MessageProveAttachment] = func(msg *blip.Message) {
2476 status, errMsg := base.ErrorAsHTTPStatus(db.ErrAttachmentNotFound)
2477 msg.Response().SetError("HTTP", status, errMsg)
2478 }
2479
2480 // Handler for when full attachment is requested
2481 bt.blipContext.HandlerForProfile[db.MessageGetAttachment] = func(msg *blip.Message) {
2482 resp := msg.Response()
2483 resp.SetBody(attachmentData)
2484 resp.SetCompressed(msg.Properties[db.BlipCompress] == "true")
2485 }
2486
2487 // Initial set up
2488 sent, _, _, err := bt.SendRev("doc1", "1-abc", []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentDataEncoded+`"}}}`), blip.Properties{})
2489 require.True(t, sent)
2490 require.NoError(t, err)
2491
2492 rt.WaitForPendingChanges()
2493
2494 // Should log:
2495 // "Peer sent prove attachment error 404 attachment not found, falling back to getAttachment for proof in doc <ud>doc1</ud> (digest sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=)"
2496 base.SetUpTestLogging(t, base.LevelInfo, base.KeyAll)
2497
2498 // Use different attachment name to bypass digest check in ForEachStubAttachment() which skips prove attachment code
2499 // Set attachment to V2 so it can be retrieved by RT successfully
2500 sent, _, _, err = bt.SendRev("doc1", "2-abc", []byte(`{"key": "val", "_attachments":{"attach":{"digest":"sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=","length":11,"stub":true,"revpos":1,"ver":2}}}`), blip.Properties{})
2501 require.True(t, sent)
2502 require.NoError(t, err)
2503
2504 rt.WaitForPendingChanges()
2505 // Check attachment is on the document
2506 body := rt.GetDocBody("doc1")
2507 assert.Equal(t, "2-abc", body.ExtractRev())
2508 resp := rt.SendAdminRequest("GET", "/{{.keyspace}}/doc1/attach", "")
2509 RequireStatus(t, resp, 200)
2510 assert.EqualValues(t, attachmentData, resp.BodyBytes())
2511}
2512
2513// Test adding / retrieving attachments
2514func TestAttachments(t *testing.T) {

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewDbConfigMethod · 0.95
CreateDatabaseMethod · 0.95
GetDatabaseMethod · 0.95
TBMethod · 0.95
SetAdminPartyMethod · 0.95
WaitForPendingChangesMethod · 0.95
GetDocBodyMethod · 0.95
SendAdminRequestMethod · 0.95
ErrorAsHTTPStatusFunction · 0.92
SetUpTestLoggingFunction · 0.92
NewRestTesterFunction · 0.85

Tested by

no test coverage detected