Source file src/net/http/internal/httpcommon/httpcommon_test.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package httpcommon_test
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  // This package is imported by the net/http package,
    16  // and therefore must not itself import net/http.
    17  func TestNoNetHttp(t *testing.T) {
    18  	files, err := filepath.Glob("*.go")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	for _, file := range files {
    23  		if strings.HasSuffix(file, "_test.go") {
    24  			continue
    25  		}
    26  		// Could use something complex like go/build or x/tools/go/packages,
    27  		// but there's no reason for "net/http" to appear (in quotes) in the source
    28  		// otherwise, so just use a simple substring search.
    29  		data, err := os.ReadFile(file)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		if bytes.Contains(data, []byte(`"net/http"`)) {
    34  			t.Errorf(`%s: cannot import "net/http"`, file)
    35  		}
    36  	}
    37  }
    38  

View as plain text