go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/catapult/upload_test.go (about) 1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package main_test 6 7 import ( 8 "io" 9 "net/http" 10 "strconv" 11 "testing" 12 13 catapult "go.fuchsia.dev/infra/cmd/catapult" 14 ) 15 16 func TestCreateUploadRequest(t *testing.T) { 17 t.Run("URL looks like scheme://host[:port]", func(t *testing.T) { 18 expectCreationSuccess( 19 "https://chromeperf.appspot.com:443", 20 "https://chromeperf.appspot.com:443/add_histograms", 21 t) 22 expectCreationSuccess( 23 "https://chromeperf.appspot.com:443/", 24 "https://chromeperf.appspot.com:443/add_histograms", 25 t) 26 expectCreationSuccess( 27 "https://chromeperf.appspot.com/", 28 "https://chromeperf.appspot.com/add_histograms", 29 t) 30 expectCreationSuccess( 31 "https://chromeperf.appspot.com", 32 "https://chromeperf.appspot.com/add_histograms", 33 t) 34 expectCreationSuccess( 35 "http://chromeperf.appspot.com", 36 "http://chromeperf.appspot.com/add_histograms", 37 t) 38 expectCreationSuccess( 39 "hello://weird.scheme.com", 40 "hello://weird.scheme.com/add_histograms", 41 t) 42 }) 43 44 t.Run("URL does not look like scheme://host[:port]", func(t *testing.T) { 45 expectCreationFailure("https://chromeperf.appspot.com/#fragment", t) 46 expectCreationFailure("https://chromeperf.appspot.com:443?arg=foo", t) 47 expectCreationFailure("https://chromeperf.appspot.com:80/non_root_path.html", t) 48 expectCreationFailure("https://chromeperf.appspot.com:443/non_root_path.html?args=also#fragmnent-too", t) 49 }) 50 } 51 52 func expectCreationFailure(inputURL string, t *testing.T) { 53 content := "{'foo': 'bar123abc'}" 54 _, err := catapult.CreateUploadRequest(inputURL, content) 55 if err == nil { 56 t.Fatalf("Expected an error. Got nil when given %q", inputURL) 57 } 58 } 59 60 func expectCreationSuccess(inputURL string, expectedURL string, t *testing.T) { 61 content := "{'foo': 'bar123abc'}" 62 req, err := catapult.CreateUploadRequest(inputURL, content) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 actualURL := req.URL.String() 68 if actualURL != expectedURL { 69 t.Errorf("expected URL %s. Got %s", expectedURL, actualURL) 70 } 71 72 if req.Method != http.MethodPost { 73 t.Errorf("expected method %s. Got %s", http.MethodPost, req.Method) 74 } 75 76 userAgent := req.Header.Get("User-Agent") 77 if userAgent != catapult.FuchsiaUserAgent { 78 t.Errorf("expected User-Agent: %s. Got %s", catapult.FuchsiaUserAgent, userAgent) 79 } 80 81 expectedContentType := "application/x-www-form-urlencoded" 82 contentType := req.Header.Get("Content-Type") 83 if contentType != expectedContentType { 84 t.Errorf("expected Content-Type: %s. Got %s", expectedContentType, contentType) 85 } 86 87 contentLength, err := strconv.ParseInt(req.Header.Get("Content-Length"), 10, 64) 88 if err != nil { 89 t.Error("failed to parse Content-Length", err) 90 } 91 actualContent, err := io.ReadAll(req.Body) 92 if err != nil { 93 t.Error("failed to read request body", err) 94 } 95 expectedContentLength := int64(len(actualContent)) 96 if contentLength != expectedContentLength { 97 t.Errorf("expected Content-Length %d for %s. Got %d", 98 expectedContentLength, actualContent, contentLength) 99 } 100 }