We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Normalize URLs

Test-driven development is a popular method of writing software. The idea is that you write tests for your code first, then you write the code that gets the tests to pass. We're going to approach this project using a bit of TDD.

Normalizing URLs

We need a function that accepts a URL string as input and returns a "normalized" URL. To "normalize" means to "make the same". For example, all of these URLs are the "same page" according to most websites and HTTP standards:

  • https://www.boot.dev/blog/path/
  • https://www.boot.dev/blog/path
  • http://www.boot.dev/blog/path/
  • http://www.boot.dev/blog/path

We want our normalizeURL() function to map all of those same inputs to a single normalized output: www.boot.dev/blog/path.

Keep in mind the normalized URL isn't going to be used to make requests, it's just going to be used to compare URLs to see if they are the same page.

Stub the Function

In test-driven development (TDD), we typically follow this flow:

  1. Stub out the function
  2. Write the tests
  3. Write the code that passes the tests

If you'd like, you can read up on the standard library's testing package and this example of a unit test before you start.

Assignment

The go test command will looks for and runs functions that end in _test.go,

func TestNormalizeURL(t *testing.T) {
	tests := []struct {
		name          string
		inputURL      string
		expected      string
	}{
		{
			name:     "remove scheme",
			inputURL: "https://www.boot.dev/blog/path",
			expected: "www.boot.dev/blog/path",
		},
        // add more test cases here
	}

	for i, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			actual, err := normalizeURL(tc.inputURL)
			if err != nil {
				t.Errorf("Test %v - '%s' FAIL: unexpected error: %v", i, tc.name, err)
				return
			}
			if actual != tc.expected {
				t.Errorf("Test %v - %s FAIL: expected URL: %v, actual: %v", i, tc.name, tc.expected, actual)
			}
		})
	}
}

Try to test all the edge-cases you can think of.

You should see some failing tests now, which is expected because you haven't filled in the normalizeURL function yet.

Run and submit the CLI tests from the root of the module.