URIs
Describes the possible expectations for Uri.
| Expectation | Negated | Summary |
|---|---|---|
IsAbsolute | IsNotAbsolute | an absolute URI |
IsFile | IsNotFile | a file URI |
IsLoopback | IsNotLoopback | references the local host |
IsUnc | IsNotUnc | a UNC path |
HasDefaultPort | DoesNotHaveDefaultPort | uses the default port of its scheme |
HasPort | negated comparison | has the expected port |
HasScheme | negated comparison | has the expected scheme |
HasHost | negated comparison | has the expected host |
HasPath | negated comparison | has the expected path |
HasFragment | negated comparison | has the expected fragment |
IsBaseOf | IsNotBaseOf | a base URI of the expected URI |
HasQueryParameter | DoesNotHaveQueryParameter | has a query parameter, optionally with a value |
Kind
You can verify what kind of URI the Uri is, or that it is not:
Uri uri = new("https://music.example.com/tracks/1");
await Expect.That(uri).IsAbsolute();
await Expect.That(uri).IsNotFile();
await Expect.That(uri).IsNotLoopback();
await Expect.That(uri).IsNotUnc();
For example, a relative URI is not absolute, so
Uri uri = new("/tracks/1", UriKind.Relative);
await Expect.That(uri).IsAbsolute();
fails with:
Expected that uri
is an absolute URI,
but it was /tracks/1
Default port
You can verify that the Uri uses the default port of its scheme, or that it does not:
await Expect.That(new Uri("https://music.example.com/tracks/1")).HasDefaultPort();
await Expect.That(new Uri("https://music.example.com:8443/tracks/1")).DoesNotHaveDefaultPort();
Port
You can verify the port of the Uri. Without an explicit port, it is the default port of the scheme:
Uri uri = new("https://localhost:5001/health");
await Expect.That(uri).HasPort().EqualTo(5001);
await Expect.That(uri).HasPort().Between(5000).And(5999);
await Expect.That(new Uri("http://localhost:4000/health")).HasPort(4000);
Scheme
You can verify the scheme of the Uri:
Uri uri = new("https://music.example.com/tracks/1");
await Expect.That(uri).HasScheme().EqualTo("https");
await Expect.That(uri).HasScheme().NotEqualTo("http");
Host
You can verify the host of the Uri:
Uri uri = new("https://music.example.com/tracks/1");
await Expect.That(uri).HasHost().EqualTo("music.example.com");
await Expect.That(uri).HasHost().EndingWith(".example.com");
Path
You can verify the URL-decoded path of the Uri:
Uri uri = new("https://music.example.com/tracks/1");
await Expect.That(uri).HasPath().EqualTo("/tracks/1");
await Expect.That(uri).HasPath().StartingWith("/tracks/");
await Expect.That(new Uri("https://music.example.com/my%20song")).HasPath().EqualTo("/my song");
Fragment
You can verify the URL-decoded fragment of the Uri, without the leading #:
Uri uri = new("https://music.example.com/tracks/1#lyrics");
await Expect.That(uri).HasFragment().EqualTo("lyrics");
await Expect.That(uri).HasFragment().NotEqualTo("credits");
Base URI
You can verify that the Uri is a base URI of another absolute URI, or that it is not:
Uri uri = new("https://music.example.com/api/");
await Expect.That(uri).IsBaseOf("https://music.example.com/api/tracks/1");
await Expect.That(uri).IsNotBaseOf(new Uri("https://other.example.com/api/tracks/1"));
Like Uri.IsBaseOf, only the part of the Uri up to its last / is considered, so https://music.example.com/api
is also a base URI of https://music.example.com/tracks/1.
Query parameters
You can verify that the Uri has a query parameter, optionally with the expected value, or that it does not have it:
Uri uri = new("https://music.example.com/tracks?page=2&genre=Jazz");
await Expect.That(uri).HasQueryParameter("page");
await Expect.That(uri).HasQueryParameter("page").WithValue("2");
await Expect.That(uri).HasQueryParameter("genre").WithValue("jazz").IgnoringCase();
await Expect.That(uri).DoesNotHaveQueryParameter("token");
Names and values are URL-decoded, and names are compared case-sensitively. WithValue supports the same
string options and
match types as comparing strings and requires the query
parameter to occur exactly once. A query parameter that occurs several times can be verified with WithValues, which
compares the values in the order in which they appear in the URI, unless InAnyOrder() is specified. It supports the
same options as comparing string collections.
You can also add expectations on the value or on all values of the query parameter:
Uri uri = new("https://music.example.com/tracks?tag=live&tag=remastered");
await Expect.That(uri).HasQueryParameter("tag").WithValues("live", "remastered");
await Expect.That(uri).HasQueryParameter("tag")
.WhoseValues(values => values.Contains("live"));