@@ -28,14 +28,53 @@ import (
2828 "os"
2929 "os/exec"
3030 "path"
31- "strconv"
3231 "strings"
3332 "testing"
3433
3534 "github.com/stretchr/testify/require"
3635 atlasv2 "go.mongodb.org/atlas-sdk/v20250312001/admin"
3736)
3837
38+ const updateSnapshotsEnvVarKey = "UPDATE_SNAPSHOTS"
39+
40+ type snapshotData struct {
41+ Body []byte
42+ Status int
43+ Method string
44+ Path string
45+
46+ Headers map [string ][]string
47+ }
48+
49+ func (d snapshotData ) Compare (v snapshotData ) int {
50+ methodCmp := strings .Compare (d .Method , v .Method )
51+ if methodCmp != 0 {
52+ return methodCmp
53+ }
54+
55+ pathCmp := strings .Compare (d .Path , v .Path )
56+ if pathCmp != 0 {
57+ return pathCmp
58+ }
59+
60+ statusCmp := d .Status - v .Status
61+ if statusCmp != 0 {
62+ return statusCmp
63+ }
64+
65+ return bytes .Compare (d .Body , v .Body )
66+ }
67+
68+ func (d snapshotData ) Write (w http.ResponseWriter ) (int , error ) {
69+ for k , v := range d .Headers {
70+ w .Header ()[k ] = v
71+ }
72+
73+ w .WriteHeader (d .Status )
74+
75+ return w .Write (d .Body )
76+ }
77+
3978// atlasE2ETestGenerator is about providing capabilities to provide projects and clusters for our e2e tests.
4079type atlasE2ETestGenerator struct {
4180 projectID string
@@ -49,7 +88,7 @@ type atlasE2ETestGenerator struct {
4988 t * testing.T
5089 fileIDs map [string ]int
5190 memoryMap map [string ]any
52- lastData map [ string ][] string
91+ lastData * snapshotData
5392}
5493
5594// Log formats its arguments using default formatting, analogous to Println,
@@ -394,11 +433,11 @@ func (g *atlasE2ETestGenerator) snapshotNameStepBack(r *http.Request) {
394433}
395434
396435func updateSnapshots () bool {
397- return isTrue (os .Getenv ("UPDATE_SNAPSHOTS" ))
436+ return isTrue (os .Getenv (updateSnapshotsEnvVarKey ))
398437}
399438
400439func skipSnapshots () bool {
401- return os .Getenv ("UPDATE_SNAPSHOTS" ) == "skip"
440+ return os .Getenv (updateSnapshotsEnvVarKey ) == "skip"
402441}
403442
404443func (g * atlasE2ETestGenerator ) loadMemory () {
@@ -439,7 +478,7 @@ func (g *atlasE2ETestGenerator) storeMemory() {
439478 }
440479}
441480
442- func (g * atlasE2ETestGenerator ) readSnapshot (r * http.Request ) [] byte {
481+ func (g * atlasE2ETestGenerator ) readSnapshot (r * http.Request ) snapshotData {
443482 g .t .Helper ()
444483
445484 filename := g .snapshotName (r )
@@ -454,7 +493,12 @@ func (g *atlasE2ETestGenerator) readSnapshot(r *http.Request) []byte {
454493 g .t .Fatal (err )
455494 }
456495
457- return buf
496+ var data snapshotData
497+ if err := json .Unmarshal (buf , & data ); err != nil {
498+ g .t .Fatal (err )
499+ }
500+
501+ return data
458502}
459503
460504func (g * atlasE2ETestGenerator ) snapshotServer () {
@@ -485,31 +529,27 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
485529 return nil // skip 401
486530 }
487531
488- data := map [string ][]string {}
489- for k , v := range resp .Header {
490- data [k ] = v
532+ data := snapshotData {
533+ Path : resp .Request .URL .Path ,
534+ Method : resp .Request .Method ,
535+ Status : resp .StatusCode ,
536+ Headers : resp .Header ,
491537 }
492538
493- data ["__path__" ] = []string {resp .Request .URL .Path }
494-
495- data ["__method__" ] = []string {resp .Request .Method }
496-
497- data ["__status__" ] = []string {strconv .Itoa (resp .StatusCode )}
498-
499539 var buf bytes.Buffer
500540 if _ , err := io .Copy (& buf , resp .Body ); err != nil {
501541 return err
502542 }
503543 resp .Body .Close ()
504544 resp .Body = io .NopCloser (& buf )
505545
506- data [ "__body__" ] = [] string { base64 . StdEncoding . EncodeToString ( buf .Bytes ())}
546+ data . Body = buf .Bytes ()
507547
508- if g .lastData != nil && data [ "__method__" ][ 0 ] == g . lastData [ "__method__" ][ 0 ] && data [ "__path__" ][ 0 ] == g .lastData [ "__path__" ][ 0 ] && data [ "__status__" ][ 0 ] == g . lastData [ "__status__" ][ 0 ] && data [ "__body__" ][ 0 ] == g . lastData [ "__body__" ][ 0 ] {
548+ if g .lastData != nil && data . Compare ( * g .lastData ) == 0 {
509549 return nil // skip same content
510550 }
511551
512- g .lastData = data
552+ g .lastData = & data
513553
514554 out , err := json .MarshalIndent (data , "" , " " )
515555 if err != nil {
@@ -529,35 +569,10 @@ func (g *atlasE2ETestGenerator) snapshotServer() {
529569 return
530570 }
531571
532- buf := g .readSnapshot (r )
533- var data map [string ][]string
534- if err := json .Unmarshal (buf , & data ); err != nil {
572+ data := g .readSnapshot (r )
573+ if _ , err := data .Write (w ); err != nil {
535574 g .t .Fatal (err )
536575 }
537-
538- for k , v := range data {
539- if k == "__body__" || k == "__status__" || k == "__method__" || k == "__path__" {
540- continue
541- }
542- w .Header ()[k ] = v
543- }
544-
545- status , err := strconv .Atoi (data ["__status__" ][0 ])
546- if err != nil {
547- g .t .Fatal (err )
548- }
549- w .WriteHeader (status )
550-
551- if data ["__body__" ] != nil {
552- body , err := base64 .StdEncoding .DecodeString (data ["__body__" ][0 ])
553- if err != nil {
554- g .t .Fatal (err )
555- }
556- _ , err = w .Write (body )
557- if err != nil {
558- g .t .Fatal (err )
559- }
560- }
561576 }))
562577
563578 g .t .Cleanup (func () {
0 commit comments