|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using DevProxy.Plugins.Utils; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace DevProxy.Integration.Tests; |
| 9 | + |
| 10 | +public class GraphUtilsTests |
| 11 | +{ |
| 12 | + [Theory] |
| 13 | + [InlineData( |
| 14 | + "https://graph.microsoft.com/v1.0/drive/root:/Documents/report.xlsx:/workbook/worksheets", |
| 15 | + "/me/drive/items/{id}/workbook/worksheets")] |
| 16 | + [InlineData( |
| 17 | + "https://graph.microsoft.com/v1.0/drives/drive-id/root:/Documents/report.xlsx", |
| 18 | + "/drives/{drives-id}/items/{id}")] |
| 19 | + [InlineData( |
| 20 | + "https://graph.microsoft.com/v1.0/groups/group-id/drive/root:/Documents/report.xlsx:/content", |
| 21 | + "/groups/{groups-id}/drive/items/{id}/content")] |
| 22 | + [InlineData( |
| 23 | + "https://graph.microsoft.com/v1.0/me/drive/root:/Documents/report.xlsx:/permissions", |
| 24 | + "/me/drive/items/{id}/permissions")] |
| 25 | + [InlineData( |
| 26 | + "https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/Documentos/informe.pdf:/content", |
| 27 | + "/sites/{site-id}/drive/items/{id}/content")] |
| 28 | + [InlineData( |
| 29 | + "https://graph.microsoft.com/beta/users/user-id/drive/root:/Documents/report.xlsx:/children", |
| 30 | + "/users/{users-id}/drive/items/{id}/children")] |
| 31 | + [InlineData( |
| 32 | + "https://graph.microsoft.com/v1.0/drives/drive-id/items/parent-id:/Documents/report.xlsx:/createUploadSession", |
| 33 | + "/drives/{drives-id}/items/{id}/createUploadSession")] |
| 34 | + public void GetTokenizedUrl_RootRelativePath_ReturnsIdAddressedUrl(string url, string expected) => |
| 35 | + Assert.Equal(expected, GraphUtils.GetTokenizedUrl(url)); |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData("My%20Files/quarterly-report%20%231.xlsx")] |
| 39 | + [InlineData("O'Brien/estimate%25s.xlsx")] |
| 40 | + [InlineData("Break%23Out/saved_game%5B1%5D.bin")] |
| 41 | + public void GetTokenizedUrl_PathWithSpecialCharacters_ReturnsIdAddressedUrl(string itemPath) |
| 42 | + { |
| 43 | + var url = $"https://graph.microsoft.com/v1.0/me/drive/root:/{itemPath}:/content"; |
| 44 | + |
| 45 | + Assert.Equal("/me/drive/items/{id}/content", GraphUtils.GetTokenizedUrl(url)); |
| 46 | + } |
| 47 | + |
| 48 | + [Theory] |
| 49 | + [InlineData( |
| 50 | + "https://graph.microsoft.com/v1.0/me/drive/items/parent-id:/Documents/report.txt:/content", |
| 51 | + "/me/drive/items/{id}/content")] |
| 52 | + [InlineData( |
| 53 | + "https://graph.microsoft.com/v1.0/me/drive/special/documents:/report.txt:/content", |
| 54 | + "/me/drive/items/{id}/content")] |
| 55 | + public void GetTokenizedUrl_ItemRelativePath_ReturnsIdAddressedUrl(string url, string expected) => |
| 56 | + Assert.Equal(expected, GraphUtils.GetTokenizedUrl(url)); |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void GetTokenizedUrl_NonPathAddressedUrl_PreservesExistingTokenization() => |
| 60 | + Assert.Equal( |
| 61 | + "/sites/{sites-id}/drive/items/{items-id}/content", |
| 62 | + GraphUtils.GetTokenizedUrl("https://graph.microsoft.com/v1.0/sites/site-id/drive/items/item-id/content")); |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void GetRequestsFromBatch_PathAddressedRequest_ReturnsIdAddressedUrl() |
| 66 | + { |
| 67 | + const string batchBody = |
| 68 | + """ |
| 69 | + { |
| 70 | + "requests": [ |
| 71 | + { |
| 72 | + "id": "1", |
| 73 | + "method": "PUT", |
| 74 | + "url": "/sites/site-id/drive/root:/Documents/report.txt:/content" |
| 75 | + }, |
| 76 | + { |
| 77 | + "id": "2", |
| 78 | + "method": "GET", |
| 79 | + "url": "/sites/site-id/drive/items/item-id" |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + """; |
| 84 | + |
| 85 | + var requests = GraphUtils.GetRequestsFromBatch(batchBody, "v1.0", "graph.microsoft.com"); |
| 86 | + |
| 87 | + Assert.Collection( |
| 88 | + requests, |
| 89 | + request => |
| 90 | + { |
| 91 | + Assert.Equal("PUT", request.Method); |
| 92 | + Assert.Equal("/sites/{sites-id}/drive/items/{id}/content", request.Url); |
| 93 | + }, |
| 94 | + request => |
| 95 | + { |
| 96 | + Assert.Equal("GET", request.Method); |
| 97 | + Assert.Equal("/sites/{sites-id}/drive/items/{items-id}", request.Url); |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
0 commit comments