-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,14 +1,38 @@ | ||
using System; | ||
using System.Web; | ||
|
||
namespace X.Web.UTM; | ||
|
||
public class UTMParser | ||
{ | ||
public UrchinTrackingModule Parse(string url) | ||
{ | ||
throw new NotImplementedException(); | ||
if (url == null) | ||
{ | ||
throw new ArgumentNullException(nameof(url)); | ||
} | ||
|
||
return Parse(new Uri(url)); | ||
} | ||
|
||
public UrchinTrackingModule Parse(Uri url) | ||
{ | ||
throw new NotImplementedException(); | ||
if (url == null) | ||
{ | ||
throw new ArgumentNullException(nameof(url)); | ||
} | ||
|
||
var queryParameters = HttpUtility.ParseQueryString(url.Query); | ||
|
||
var utm = new UrchinTrackingModule | ||
{ | ||
Source = queryParameters["utm_source"] ?? string.Empty, | ||
Medium = queryParameters["utm_medium"] ?? string.Empty, | ||
Campaign = queryParameters["utm_campaign"] ?? string.Empty, | ||
Term = queryParameters["utm_term"] ?? string.Empty, | ||
Content = queryParameters["utm_content"] ?? string.Empty | ||
}; | ||
|
||
return utm; | ||
} | ||
} |
This file contains 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,58 @@ | ||
using System; | ||
using Xunit; | ||
using X.Web.UTM; | ||
|
||
namespace Tests.X.Web.UTM; | ||
|
||
public class UTMParserTests | ||
{ | ||
[Fact] | ||
public void Parse_ValidUrlWithAllParameters_ReturnsCorrectUTM() | ||
{ | ||
var parser = new UTMParser(); | ||
var testUrl = "http://example.com?utm_source=google&utm_medium=cpc&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content"; | ||
|
||
var result = parser.Parse(testUrl); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("google", result.Source); | ||
Assert.Equal("cpc", result.Medium); | ||
Assert.Equal("test_campaign", result.Campaign); | ||
Assert.Equal("test_term", result.Term); | ||
Assert.Equal("test_content", result.Content); | ||
} | ||
|
||
[Fact] | ||
public void Parse_ValidUriWithAllParameters_ReturnsCorrectUTM() | ||
{ | ||
var parser = new UTMParser(); | ||
var testUri = new Uri("http://example.com?utm_source=google&utm_medium=cpc&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content"); | ||
|
||
var result = parser.Parse(testUri); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal("google", result.Source); | ||
Assert.Equal("cpc", result.Medium); | ||
Assert.Equal("test_campaign", result.Campaign); | ||
Assert.Equal("test_term", result.Term); | ||
Assert.Equal("test_content", result.Content); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://example.com", "", "", "", "", "")] | ||
[InlineData("http://example.com?utm_source=", "", "", "", "", "")] | ||
[InlineData("http://example.com?utm_medium=&utm_campaign=&utm_term=&utm_content=", "", "", "", "", "")] | ||
public void Parse_UrlWithMissingOrEmptyUtmParameters_ReturnsUtmWithEmptyFields(string url, string expectedSource, string expectedMedium, string expectedCampaign, string expectedTerm, string expectedContent) | ||
{ | ||
var parser = new UTMParser(); | ||
|
||
var result = parser.Parse(url); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(expectedSource, result.Source); | ||
Assert.Equal(expectedMedium, result.Medium); | ||
Assert.Equal(expectedCampaign, result.Campaign); | ||
Assert.Equal(expectedTerm, result.Term); | ||
Assert.Equal(expectedContent, result.Content); | ||
} | ||
} |