CodeSampleX

示例

net/http go1.26.5: Expose that method-aware mux patterns in net/http silently change which handler matches.

已验证示例 — golang net/http go1.26.5: Expose that method-aware mux patterns in net/http silently change which handler matches. contract 在 go 1.26 · linux…

sha256:0d4b034698a0346c1a1a03f0dab522b49ea18f19b7ef59476dd07f92911c1b96

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 go linux x64 go go go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-16
go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-18

案例

HOW
目标
Expose that method-aware mux patterns in net/http silently change which handler matches.
包
符号
  • ServeMux
环境
go
创建时间
2026-08-16T13:07:01Z

契约

  1. Registering `HandleFunc("GET /resource", ...)` and `HandleFunc("POST /resource", ...)` must allow GET and POST only, while a PUT request to `/resource` must fail with 405.
  2. A PUT request to `/resource` must return `405 Method Not Allowed` and include `GET` and `POST` in the `Allow` header.

文件

  • NOTES.md
  • csx.json
  • go.mod
  • net_http_methodmux_test.go

下载源代码构件 (tar.gz)

源代码

NOTES.md
# Notes

`search_known_solution` returned a compatible existing sample for `pkg:golang/net/http@go1.26.5` (`sampleId: sha256:a5d71aedbea662543b6d6b4b60bce0ccd1b8876c606797a1bbab8655f25ddf1f`) proving `http.Client.Timeout` covers the full request lifecycle, so I selected a different trap in `net/http` using `ServeMux`.

On old behavior, `GET /resource` and `POST /resource` were not method-aware route entries, so requests to `/resource` were handled as a normal path and could return green-build `404` instead of method-specific routing.
csx.json
{"case":{"believed":"A method token in a ServeMux pattern, such as `GET /resource`, is just part of the path and does not affect routing.","caseId":"case:sha256:c3a7a6c04fe5937b0c3deab034e49219de076dd51ef8207fba64ff404ed64e79","contract":["Registering `HandleFunc(\"GET /resource\", ...)` and `HandleFunc(\"POST /resource\", ...)` must allow GET and POST only, while a PUT request to `/resource` must fail with 405.","A PUT request to `/resource` must return `405 Method Not Allowed` and include `GET` and `POST` in the `Allow` header."],"goal":"Expose that method-aware mux patterns in net/http silently change which handler matches.","kind":"HOW","packages":["pkg:golang/net/http@go1.26.5"],"schemaVersion":1,"symbols":["ServeMux"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","ecosystem":"golang","executionContext":"go","language":"go","os":"linux","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/net/http@go1.26.5"],"schemaVersion":1,"symbols":["ServeMux"],"verifierAdapter":"golang@1"}
go.mod
module codesamplex

go 1.26
net_http_methodmux_test.go
package codesamplex

import (
	"net/http"
	"net/http/httptest"
	"net/http/httptrace"
	"strings"
	"testing"
)

func TestServeMuxMethodRoutes(t *testing.T) {
	mux := http.NewServeMux()
	mux.HandleFunc("GET /resource", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})
	mux.HandleFunc("POST /resource", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusCreated)
	})

	server := httptest.NewServer(mux)
	defer server.Close()

	type call struct {
		method      string
		wantStatus  int
		wantAllowed []string
	}
	cases := []call{
		{method: "GET", wantStatus: http.StatusOK},
		{method: "POST", wantStatus: http.StatusCreated},
		{method: "PUT", wantStatus: http.StatusMethodNotAllowed, wantAllowed: []string{"GET", "POST"}},
	}

	client := server.Client()
	client.Transport = http.DefaultTransport
	// use an identity Trace to avoid test flakiness from connection pooling differences
	trace := &httptrace.ClientTrace{}
	for _, c := range cases {
		req, err := http.NewRequest(c.method, server.URL+"/resource", nil)
		if err != nil {
			t.Fatalf("request build failed: %v", err)
		}
		req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
		res, err := client.Do(req)
		if err != nil {
			t.Fatalf("request failed: %v", err)
		}
		if res.StatusCode != c.wantStatus {
			t.Fatalf("%s /resource = %d, want %d", c.method, res.StatusCode, c.wantStatus)
		}
		if c.method == "PUT" {
			allow := res.Header.Get("Allow")
			for _, m := range c.wantAllowed {
				if !strings.Contains(allow, m) {
					t.Fatalf("Allow header %q missing %s", allow, m)
				}
			}
		}
		res.Body.Close()
	}
}

原始种子者

csx-seed