@@ -13376,14 +13376,64 @@ TEST(pipeline_route_prefix_bridge_is_order_free_and_root_bridges_nothing) {
1337613376 PASS();
1337713377}
1337813378
13379+ /* Route nodes whose qualified name contains `part`. */
13380+ static int route_nodes_containing(cbm_store_t *s, const char *project, const char *part) {
13381+ cbm_node_t *routes = NULL;
13382+ int route_count = 0;
13383+ if (cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count) != CBM_STORE_OK) {
13384+ return -1;
13385+ }
13386+ int matches = 0;
13387+ for (int i = 0; i < route_count; i++) {
13388+ if (routes[i].qualified_name && strstr(routes[i].qualified_name, part)) {
13389+ matches++;
13390+ }
13391+ }
13392+ if (routes) {
13393+ cbm_store_free_nodes(routes, route_count);
13394+ }
13395+ return matches;
13396+ }
13397+
13398+ /* CALLS edges out of the function `source_name` into a Route node: the edge a
13399+ * route registration emits beside its Route (an HTTP client call's edge to a
13400+ * Route is HTTP_CALLS instead). */
13401+ static int calls_into_route_count(cbm_store_t *s, const char *project, const char *source_name) {
13402+ cbm_edge_t *edges = NULL;
13403+ int edge_count = 0;
13404+ if (cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count) != CBM_STORE_OK) {
13405+ return -1;
13406+ }
13407+ int matches = 0;
13408+ for (int i = 0; i < edge_count; i++) {
13409+ cbm_node_t source = {0};
13410+ cbm_node_t target = {0};
13411+ int source_ok = cbm_store_find_node_by_id(s, edges[i].source_id, &source) == CBM_STORE_OK;
13412+ int target_ok = cbm_store_find_node_by_id(s, edges[i].target_id, &target) == CBM_STORE_OK;
13413+ if (source_ok && target_ok && source.name && target.label &&
13414+ strcmp(source.name, source_name) == 0 && strcmp(target.label, "Route") == 0) {
13415+ matches++;
13416+ }
13417+ cbm_node_free_fields(&source);
13418+ cbm_node_free_fields(&target);
13419+ }
13420+ if (edges) {
13421+ cbm_store_free_edges(edges, edge_count);
13422+ }
13423+ return matches;
13424+ }
13425+
1337913426/* Issue #2537: since Go 1.22 a ServeMux pattern may name a method and a host
1338013427 * before the path ("POST /v1/payments", "GET api.example.com/v1/x"). Such a
1338113428 * registration minted no Route and no HANDLES edge, because every registration
1338213429 * gate wanted the first argument to open with a slash, while the same file
1338313430 * written with "/v1/payments" had both. The method now comes from the pattern
1338413431 * and a host is dropped from the path, in the sequential and the parallel pass
1338513432 * alike. Only Go reads that grammar: a Python registration of "GET /py" is still
13386- * no route. */
13433+ * no route. Only a ServeMux registration (Handle / HandleFunc) is read with it:
13434+ * another route-verb call names no pattern and mints no Route, neither
13435+ * `st.Get("users/123")` on a project method (which keeps its CALLS edge) nor
13436+ * `c.Get("https://api.example.com/v1/x")` on a client. */
1338713437TEST(pipeline_go_servemux_method_patterns_issue2537) {
1338813438 char tmp[256];
1338913439 snprintf(tmp, sizeof(tmp), "/tmp/cbm_servemux_XXXXXX");
@@ -13401,6 +13451,16 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1340113451 "func hostOnly(w http.ResponseWriter, r *http.Request) {}\n"
1340213452 "func legacyList(w http.ResponseWriter, r *http.Request) {}\n"
1340313453 "\n"
13454+ "type Store struct{ m map[string]string }\n"
13455+ "\n"
13456+ "func (s *Store) Get(k string) string { return s.m[k] }\n"
13457+ "\n"
13458+ "func lookup(st *Store) string { return st.Get(\"users/123\") }\n"
13459+ "\n"
13460+ "func fetchRemote(c *http.Client) {\n"
13461+ "\t_, _ = c.Get(\"https://api.example.com/v1/x\")\n"
13462+ "}\n"
13463+ "\n"
1340413464 "func main() {\n"
1340513465 "\tmux := http.NewServeMux()\n"
1340613466 "\tmux.HandleFunc(\"POST /v1/payments\", createPayment)\n"
@@ -13438,6 +13498,11 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1343813498 int legacy[2] = {-1, -1};
1343913499 int py_any[2] = {-1, -1};
1344013500 int py_get[2] = {-1, -1};
13501+ int store_calls[2] = {-1, -1};
13502+ int store_route[2] = {-1, -1};
13503+ int lookup_reg[2] = {-1, -1};
13504+ int remote_route[2] = {-1, -1};
13505+ int remote_reg[2] = {-1, -1};
1344113506 for (int mode = 0; mode < 2; mode++) {
1344213507 if (mode == 0) {
1344313508 cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
@@ -13463,6 +13528,11 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1346313528 legacy[mode] = handles_to_route(s, project, "legacyList", "__route__ANY__/v1/legacy");
1346413529 py_any[mode] = handles_to_route(s, project, "py_view", "__route__ANY__/py");
1346513530 py_get[mode] = handles_to_route(s, project, "py_view", "__route__GET__/py");
13531+ store_calls[mode] = named_edge_count(s, project, "CALLS", "lookup", "Get");
13532+ store_route[mode] = route_nodes_containing(s, project, "__route__GET__/123");
13533+ lookup_reg[mode] = calls_into_route_count(s, project, "lookup");
13534+ remote_route[mode] = route_nodes_containing(s, project, "//api.example.com");
13535+ remote_reg[mode] = calls_into_route_count(s, project, "fetchRemote");
1346613536 }
1346713537 if (s) {
1346813538 cbm_store_close(s);
@@ -13496,6 +13566,14 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1349613566 /* another language's "GET /py" is not read as a ServeMux pattern */
1349713567 ASSERT_EQ(py_any[mode], 0);
1349813568 ASSERT_EQ(py_get[mode], 0);
13569+ /* a project method named Get keeps its CALLS edge; "users/123" is no
13570+ * host + path */
13571+ ASSERT_EQ(store_calls[mode], 1);
13572+ ASSERT_EQ(store_route[mode], 0);
13573+ ASSERT_EQ(lookup_reg[mode], 0);
13574+ /* a client Get on a URL registers no route: "https:" is no host */
13575+ ASSERT_EQ(remote_route[mode], 0);
13576+ ASSERT_EQ(remote_reg[mode], 0);
1349913577 }
1350013578 PASS();
1350113579}
0 commit comments