Repository navigation
Fix bounded OneDrive search pagination #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package graphapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSearchDriveCollectsBoundedPages(t *testing.T) { | ||
| requests := 0 | ||
| client := testGraphClient(t, func(req *http.Request) *http.Response { | ||
| requests++ | ||
| switch requests { | ||
| case 1: | ||
| if got := req.URL.Path; got != "/v1.0/drives/drive-id/search(q='budget')" { | ||
| t.Errorf("first request path = %q", got) | ||
| } | ||
| if got := req.URL.Query().Get("$top"); got != "3" { | ||
| t.Errorf("first request top = %q, want 3", got) | ||
| } | ||
| return graphJSONResponse(req, `{"value":[{"id":"one","name":"one.docx"},{"id":"two","name":"two.xlsx"}],"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=opaque%20cursor&foo=a%2Bb&$top=3"}`) | ||
| case 2: | ||
| const wantQuery = "$skiptoken=opaque%20cursor&foo=a%2Bb&$top=3" | ||
| if got := req.URL.RawQuery; got != wantQuery { | ||
| t.Errorf("continuation query = %q, want opaque query %q", got, wantQuery) | ||
| } | ||
| return graphJSONResponse(req, `{"value":[{"id":"three","name":"three.pptx"},{"id":"four","name":"four.pdf"}]}`) | ||
| default: | ||
| t.Fatalf("unexpected request %d: %s", requests, req.URL) | ||
| return nil | ||
| } | ||
| }) | ||
|
|
||
| items, err := client.SearchDrive(context.Background(), "drive-id", "budget", 3) | ||
| if err != nil { | ||
| t.Fatalf("SearchDrive() error = %v", err) | ||
| } | ||
| if requests != 2 { | ||
| t.Fatalf("request count = %d, want 2", requests) | ||
| } | ||
| got := []string{items[0].ID, items[1].ID, items[2].ID} | ||
| if !reflect.DeepEqual(got, []string{"one", "two", "three"}) { | ||
| t.Errorf("item IDs = %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestSearchDriveRejectsUnsafeContinuation(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| nextLink string | ||
| want string | ||
| }{ | ||
| {name: "host", nextLink: "https://example.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=next", want: "unexpected host"}, | ||
| {name: "drive", nextLink: "https://graph.microsoft.com/v1.0/drives/other-drive/search(q='budget')?$skiptoken=next", want: "outside expected collection"}, | ||
| {name: "query", nextLink: "https://graph.microsoft.com/v1.0/drives/drive-id/search(q='other')?$skiptoken=next", want: "outside expected collection"}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| requests := 0 | ||
| client := testGraphClient(t, func(req *http.Request) *http.Response { | ||
| requests++ | ||
| return graphJSONResponse(req, `{"value":[{"id":"one"}],"@odata.nextLink":"`+tc.nextLink+`"}`) | ||
| }) | ||
|
|
||
| items, err := client.SearchDrive(context.Background(), "drive-id", "budget", 2) | ||
| if err == nil || !strings.Contains(err.Error(), tc.want) { | ||
| t.Fatalf("SearchDrive() error = %v, want %q", err, tc.want) | ||
| } | ||
| if items != nil { | ||
| t.Fatalf("SearchDrive() items = %v, want nil", items) | ||
| } | ||
| if requests != 1 { | ||
| t.Errorf("request count = %d, want 1", requests) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSearchDriveRejectsBadPageProgress(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| firstBody string | ||
| secondBody string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "empty continuation page", | ||
| firstBody: `{"value":[],"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=next"}`, | ||
| want: "made no progress", | ||
| }, | ||
| { | ||
| name: "duplicate item", | ||
| firstBody: `{"value":[{"id":"one"}],"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=next"}`, | ||
| secondBody: `{"value":[{"id":"one"}]}`, | ||
| want: "duplicate item ID", | ||
| }, | ||
| { | ||
| name: "repeated URL", | ||
| firstBody: `{"value":[{"id":"one"}],"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=next"}`, | ||
| secondBody: `{"value":[{"id":"two"}],"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/drive-id/search(q='budget')?$skiptoken=next"}`, | ||
| want: "repeated a previous URL", | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| requests := 0 | ||
| client := testGraphClient(t, func(req *http.Request) *http.Response { | ||
| requests++ | ||
| body := tc.firstBody | ||
| if requests == 2 { | ||
| body = tc.secondBody | ||
| } | ||
| return graphJSONResponse(req, body) | ||
| }) | ||
|
|
||
| items, err := client.SearchDrive(context.Background(), "drive-id", "budget", 3) | ||
| if err == nil || !strings.Contains(err.Error(), tc.want) { | ||
| t.Fatalf("SearchDrive() error = %v, want %q", err, tc.want) | ||
| } | ||
| if items != nil { | ||
| t.Fatalf("SearchDrive() items = %v, want nil", items) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use the remaining result limit in the continuation expectation.
After the first page returns two items,
SearchDriveneeds one more item. This test currently requires$top=3, so it accepts a continuation request that exceeds the caller's remaining limit. Preserve$skiptokenandfoo, but expect$top=1.Proposed test update
📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not changing this. Microsoft Graph documents @odata.nextLink as opaque, so replacing
$top=3with$top=1would reintroduce the exact issue fixed from the prior review. SearchDrive enforces the remaining bound locally and returns after collecting the third item.