1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
|
package httpmock
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"github.com/jarcoal/httpmock/internal"
)
const regexpPrefix = "=~"
// NoResponderFound is returned when no responders are found for a
// given HTTP method and URL.
var NoResponderFound = internal.NoResponderFound
var stdMethods = map[string]bool{
"CONNECT": true, // Section 9.9
"DELETE": true, // Section 9.7
"GET": true, // Section 9.3
"HEAD": true, // Section 9.4
"OPTIONS": true, // Section 9.2
"POST": true, // Section 9.5
"PUT": true, // Section 9.6
"TRACE": true, // Section 9.8
}
// methodProbablyWrong returns true if method has probably wrong case.
func methodProbablyWrong(method string) bool {
return !stdMethods[method] && stdMethods[strings.ToUpper(method)]
}
// ConnectionFailure is a responder that returns a connection failure.
// This is the default responder and is called when no other matching
// responder is found. See [RegisterNoResponder] to override this
// default behavior.
func ConnectionFailure(*http.Request) (*http.Response, error) {
return nil, NoResponderFound
}
// NewMockTransport creates a new [*MockTransport] with no responders.
func NewMockTransport() *MockTransport {
return &MockTransport{
responders: make(map[internal.RouteKey]matchResponders),
callCountInfo: make(map[matchRouteKey]int),
}
}
type regexpResponder struct {
origRx string
method string
rx *regexp.Regexp
responders matchResponders
}
// MockTransport implements [http.RoundTripper] interface, which
// fulfills single HTTP requests issued by an [http.Client]. This
// implementation doesn't actually make the call, instead deferring to
// the registered list of responders.
type MockTransport struct {
// DontCheckMethod disables standard methods check. By default, if
// a responder is registered using a lower-cased method among CONNECT,
// DELETE, GET, HEAD, OPTIONS, POST, PUT and TRACE, a panic occurs
// as it is probably a mistake.
DontCheckMethod bool
mu sync.RWMutex
responders map[internal.RouteKey]matchResponders
regexpResponders []regexpResponder
noResponder Responder
callCountInfo map[matchRouteKey]int
totalCallCount int
}
var findForKey = []func(*MockTransport, internal.RouteKey) respondersFound{
(*MockTransport).respondersForKey, // Exact match
(*MockTransport).regexpRespondersForKey, // Regexp match
}
type respondersFound struct {
responders matchResponders
key, respKey internal.RouteKey
submatches []string
}
func (m *MockTransport) findResponders(method string, url *url.URL, fromIdx int) (
found respondersFound,
findForKeyIndex int,
) {
urlStr := url.String()
key := internal.RouteKey{
Method: method,
}
for findForKeyIndex = fromIdx; findForKeyIndex <= len(findForKey)-1; findForKeyIndex++ {
getResponders := findForKey[findForKeyIndex]
// try and get a responder that matches the method and URL with
// query params untouched: http://z.tld/path?q...
key.URL = urlStr
found = getResponders(m, key)
if found.responders != nil {
break
}
// if we weren't able to find some responders, try with the URL *and*
// sorted query params
query := sortedQuery(url.Query())
if query != "" {
// Replace unsorted query params by sorted ones:
// http://z.tld/path?sorted_q...
key.URL = strings.Replace(urlStr, url.RawQuery, query, 1)
found = getResponders(m, key)
if found.responders != nil {
break
}
}
// if we weren't able to find some responders, try without any query params
strippedURL := *url
strippedURL.RawQuery = ""
strippedURL.Fragment = ""
// go1.6 does not handle URL.ForceQuery, so in case it is set in go>1.6,
// remove the "?" manually if present.
surl := strings.TrimSuffix(strippedURL.String(), "?")
hasQueryString := urlStr != surl
// if the URL contains a querystring then we strip off the
// querystring and try again: http://z.tld/path
if hasQueryString {
key.URL = surl
found = getResponders(m, key)
if found.responders != nil {
break
}
}
// if we weren't able to find some responders for the full URL, try with
// the path part only
pathAlone := url.RawPath
if pathAlone == "" {
pathAlone = url.Path
}
// First with unsorted querystring: /path?q...
if hasQueryString {
key.URL = pathAlone + strings.TrimPrefix(urlStr, surl) // concat after-path part
found = getResponders(m, key)
if found.responders != nil {
break
}
// Then with sorted querystring: /path?sorted_q...
key.URL = pathAlone + "?" + sortedQuery(url.Query())
if url.Fragment != "" {
key.URL += "#" + url.Fragment
}
found = getResponders(m, key)
if found.responders != nil {
break
}
}
// Then using path alone: /path
key.URL = pathAlone
found = getResponders(m, key)
if found.responders != nil {
break
}
}
found.key = key
return
}
// suggestResponder is typically called after a findResponders failure
// to suggest a user mistake.
func (m *MockTransport) suggestResponder(method string, url *url.URL) *internal.ErrorNoResponderFoundMistake {
// Responder not found, try to detect some common user mistakes on
// method then on path
var found respondersFound
// On method first
if methodProbablyWrong(method) {
// Get → GET
found, _ = m.findResponders(strings.ToUpper(method), url, 0)
}
if found.responders == nil {
// Search for any other method
found, _ = m.findResponders("", url, 0)
}
if found.responders != nil {
return &internal.ErrorNoResponderFoundMistake{
Kind: "method",
Orig: method,
Suggested: found.respKey.Method,
}
}
// Then on path
if strings.HasSuffix(url.Path, "/") {
// Try without final "/"
u := *url
u.Path = strings.TrimSuffix(u.Path, "/")
found, _ = m.findResponders("", &u, 0)
}
if found.responders == nil && strings.Contains(url.Path, "//") {
// Try without double "/"
u := *url
squash := false
u.Path = strings.Map(func(r rune) rune {
if r == '/' {
if squash {
return -1
}
squash = true
} else {
squash = false
}
return r
}, u.Path)
found, _ = m.findResponders("", &u, 0)
}
if found.responders != nil {
return &internal.ErrorNoResponderFoundMistake{
Kind: "URL",
Orig: url.String(),
Suggested: found.respKey.URL,
}
}
return nil
}
// RoundTrip receives HTTP requests and routes them to the appropriate
// responder. It is required to implement the [http.RoundTripper]
// interface. You will not interact with this directly, instead the
// [*http.Client] you are using will call it for you.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
method := req.Method
if method == "" {
// http.Request.Method is documented to default to GET:
method = http.MethodGet
}
var (
suggested *internal.ErrorNoResponderFoundMistake
responder Responder
fail bool
found respondersFound
findIdx int
)
for fromFindIdx := 0; ; {
found, findIdx = m.findResponders(method, req.URL, fromFindIdx)
if found.responders == nil {
if suggested == nil { // a suggestion is already available, no need of a new one
suggested = m.suggestResponder(method, req.URL)
fail = true
}
break
}
// we found some responders, check for one matcher
mr := func() *matchResponder {
m.mu.RLock()
defer m.mu.RUnlock()
return found.responders.findMatchResponder(req)
}()
if mr == nil {
if suggested == nil {
// a suggestion is not already available, do it now
fail = true
if len(found.responders) == 1 {
suggested = &internal.ErrorNoResponderFoundMistake{
Kind: "matcher",
Suggested: fmt.Sprintf("matcher %q", found.responders[0].matcher.name),
}
} else {
names := make([]string, len(found.responders))
for i, mr := range found.responders {
names[i] = mr.matcher.name
}
suggested = &internal.ErrorNoResponderFoundMistake{
Kind: "matcher",
Suggested: fmt.Sprintf("%d matchers: %q", len(found.responders), names),
}
}
}
// No Matcher found for exact match, retry for regexp match
if findIdx < len(findForKey)-1 {
fromFindIdx = findIdx + 1
continue
}
break
}
// OK responder found
fail = false
responder = mr.responder
m.mu.Lock()
m.callCountInfo[matchRouteKey{RouteKey: found.key, name: mr.matcher.name}]++
if found.key != found.respKey {
m.callCountInfo[matchRouteKey{RouteKey: found.respKey, name: mr.matcher.name}]++
}
m.totalCallCount++
m.mu.Unlock()
break
}
if fail {
m.mu.Lock()
if m.noResponder != nil {
// we didn't find a responder, so fire the 'no responder' responder
m.callCountInfo[matchRouteKey{RouteKey: internal.NoResponder}]++
m.totalCallCount++
// give a hint to NewNotFoundResponder() if it is a possible
// method or URL error, or missing matcher
if suggested != nil {
req = req.WithContext(context.WithValue(req.Context(), suggestedKey, &suggestedInfo{
kind: suggested.Kind,
suggested: suggested.Suggested,
}))
}
responder = m.noResponder
}
m.mu.Unlock()
}
if responder == nil {
if suggested != nil {
return nil, suggested
}
return ConnectionFailure(req)
}
return runCancelable(responder, internal.SetSubmatches(req, found.submatches))
}
func (m *MockTransport) numResponders() int {
num := 0
for _, mrs := range m.responders {
num += len(mrs)
}
for _, rr := range m.regexpResponders {
num += len(rr.responders)
}
return num
}
// NumResponders returns the number of responders currently in use.
// The responder registered with [MockTransport.RegisterNoResponder]
// is not taken into account.
func (m *MockTransport) NumResponders() int {
m.mu.RLock()
defer m.mu.RUnlock()
return m.numResponders()
}
// Responders returns the list of currently registered responders.
// Each responder is listed as a string containing "METHOD URL".
// Non-regexp responders are listed first in alphabetical order
// (sorted by URL then METHOD), then regexp responders in the order
// they have been registered.
//
// The responder registered with [MockTransport.RegisterNoResponder]
// is not listed.
func (m *MockTransport) Responders() []string {
m.mu.RLock()
defer m.mu.RUnlock()
rks := make([]internal.RouteKey, 0, len(m.responders))
for rk := range m.responders {
rks = append(rks, rk)
}
sort.Slice(rks, func(i, j int) bool {
if rks[i].URL == rks[j].URL {
return rks[i].Method < rks[j].Method
}
return rks[i].URL < rks[j].URL
})
rs := make([]string, 0, m.numResponders())
for _, rk := range rks {
for _, mr := range m.responders[rk] {
rs = append(rs, matchRouteKey{
RouteKey: rk,
name: mr.matcher.name,
}.String())
}
}
for _, rr := range m.regexpResponders {
for _, mr := range rr.responders {
rs = append(rs, matchRouteKey{
RouteKey: internal.RouteKey{
Method: rr.method,
URL: rr.origRx,
},
name: mr.matcher.name,
}.String())
}
}
return rs
}
func runCancelable(responder Responder, req *http.Request) (*http.Response, error) {
ctx := req.Context()
if req.Cancel == nil && ctx.Done() == nil { // nolint: staticcheck
resp, err := responder(req)
return resp, internal.CheckStackTracer(req, err)
}
// Set up a goroutine that translates a close(req.Cancel) into a
// "request canceled" error, and another one that runs the
// responder. Then race them: first to the result channel wins.
type result struct {
response *http.Response
err error
}
resultch := make(chan result, 1)
done := make(chan struct{}, 1)
go func() {
select {
case <-req.Cancel: // nolint: staticcheck
resultch <- result{
response: nil,
err: errors.New("request canceled"),
}
case <-ctx.Done():
resultch <- result{
response: nil,
err: ctx.Err(),
}
case <-done:
}
}()
go func() {
defer func() {
if err := recover(); err != nil {
resultch <- result{
response: nil,
err: fmt.Errorf("panic in responder: got %q", err),
}
}
}()
response, err := responder(req)
resultch <- result{
response: response,
err: err,
}
}()
r := <-resultch
// if a cancel() issued from context.WithCancel() or a
// close(req.Cancel) are never coming, we'll need to unblock the
// first goroutine.
done <- struct{}{}
return r.response, internal.CheckStackTracer(req, r.err)
}
// respondersForKey returns a responder for a given key.
func (m *MockTransport) respondersForKey(key internal.RouteKey) respondersFound {
m.mu.RLock()
defer m.mu.RUnlock()
if key.Method != "" {
return respondersFound{
responders: m.responders[key],
respKey: key,
}
}
for k, resp := range m.responders {
if key.URL == k.URL {
return respondersFound{
responders: resp,
respKey: k,
}
}
}
return respondersFound{}
}
// respondersForKeyUsingRegexp returns the first responder matching a
// given key using regexps.
func (m *MockTransport) regexpRespondersForKey(key internal.RouteKey) respondersFound {
m.mu.RLock()
defer m.mu.RUnlock()
for _, regInfo := range m.regexpResponders {
if key.Method == "" || regInfo.method == key.Method {
if sm := regInfo.rx.FindStringSubmatch(key.URL); sm != nil {
if len(sm) == 1 {
sm = nil
} else {
sm = sm[1:]
}
return respondersFound{
responders: regInfo.responders,
respKey: internal.RouteKey{
Method: regInfo.method,
URL: regInfo.origRx,
},
submatches: sm,
}
}
}
}
return respondersFound{}
}
func isRegexpURL(url string) bool {
return strings.HasPrefix(url, regexpPrefix)
}
func (m *MockTransport) checkMethod(method string, matcher Matcher) {
if !m.DontCheckMethod && methodProbablyWrong(method) {
panic(fmt.Sprintf("You probably want to use method %q instead of %q? If not and so want to disable this check, set MockTransport.DontCheckMethod field to true",
strings.ToUpper(method),
method,
))
}
}
// RegisterMatcherResponder adds a new responder, associated with a given
// HTTP method, URL (or path) and [Matcher].
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// If url contains query parameters, their order matters as well as
// their content. All following URLs are here considered as different:
//
// http://z.tld?a=1&b=1
// http://z.tld?b=1&a=1
// http://z.tld?a&b
// http://z.tld?a=&b=
//
// If url begins with "=~", the following chars are considered as a
// regular expression. If this regexp can not be compiled, it panics.
// Note that the "=~" prefix remains in statistics returned by
// [MockTransport.GetCallCountInfo]. As 2 regexps can match the same
// URL, the regexp responders are tested in the order they are
// registered. Registering an already existing regexp responder (same
// method & same regexp string) replaces its responder, but does not
// change its position.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [MockTransport.GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.GetCallCountInfo]. It does nothing if it does not
// already exist. The original matcher can be passed but also a new
// [Matcher] with the same name and a nil match function as in:
//
// NewMatcher("original matcher name", nil)
//
// See [MockTransport.RegisterRegexpMatcherResponder] to directly pass a
// [*regexp.Regexp].
//
// If several responders are registered for a same method and url
// couple, but with different matchers, they are ordered depending on
// the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [MockTransport.RegisterResponder]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See also [MockTransport.RegisterResponder] if a matcher is not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func (m *MockTransport) RegisterMatcherResponder(method, url string, matcher Matcher, responder Responder) {
m.checkMethod(method, matcher)
mr := matchResponder{
matcher: matcher,
responder: responder,
}
if isRegexpURL(url) {
rr := regexpResponder{
origRx: url,
method: method,
rx: regexp.MustCompile(url[2:]),
responders: matchResponders{mr},
}
m.registerRegexpResponder(rr)
return
}
key := internal.RouteKey{
Method: method,
URL: url,
}
m.mu.Lock()
if responder == nil {
if mrs := m.responders[key].remove(matcher.name); mrs == nil {
delete(m.responders, key)
} else {
m.responders[key] = mrs
}
delete(m.callCountInfo, matchRouteKey{RouteKey: key, name: matcher.name})
} else {
m.responders[key] = m.responders[key].add(mr)
m.callCountInfo[matchRouteKey{RouteKey: key, name: matcher.name}] = 0
}
m.mu.Unlock()
}
// RegisterResponder adds a new responder, associated with a given
// HTTP method and URL (or path).
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// If url contains query parameters, their order matters as well as
// their content. All following URLs are here considered as different:
//
// http://z.tld?a=1&b=1
// http://z.tld?b=1&a=1
// http://z.tld?a&b
// http://z.tld?a=&b=
//
// If url begins with "=~", the following chars are considered as a
// regular expression. If this regexp can not be compiled, it panics.
// Note that the "=~" prefix remains in statistics returned by
// [MockTransport.GetCallCountInfo]. As 2 regexps can match the same
// URL, the regexp responders are tested in the order they are
// registered. Registering an already existing regexp responder (same
// method & same regexp string) replaces its responder, but does not
// change its position.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [MockTransport.GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.GetCallCountInfo]. It does nothing if it does not
// already exist.
//
// See [MockTransport.RegisterRegexpResponder] to directly pass a
// [*regexp.Regexp].
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See [MockTransport.RegisterMatcherResponder] to also match on
// request header and/or body.
func (m *MockTransport) RegisterResponder(method, url string, responder Responder) {
m.RegisterMatcherResponder(method, url, Matcher{}, responder)
}
// It is the caller responsibility that len(rxResp.reponders) == 1.
func (m *MockTransport) registerRegexpResponder(rxResp regexpResponder) {
m.mu.Lock()
defer m.mu.Unlock()
mr := rxResp.responders[0]
found:
for {
for i, rr := range m.regexpResponders {
if rr.method == rxResp.method && rr.origRx == rxResp.origRx {
if mr.responder == nil {
rr.responders = rr.responders.remove(mr.matcher.name)
if rr.responders == nil {
copy(m.regexpResponders[:i], m.regexpResponders[i+1:])
m.regexpResponders[len(m.regexpResponders)-1] = regexpResponder{}
m.regexpResponders = m.regexpResponders[:len(m.regexpResponders)-1]
} else {
m.regexpResponders[i] = rr
}
} else {
rr.responders = rr.responders.add(mr)
m.regexpResponders[i] = rr
}
break found
}
}
if mr.responder != nil {
m.regexpResponders = append(m.regexpResponders, rxResp)
}
break // nolint: staticcheck
}
mrk := matchRouteKey{
RouteKey: internal.RouteKey{
Method: rxResp.method,
URL: rxResp.origRx,
},
name: mr.matcher.name,
}
if mr.responder == nil {
delete(m.callCountInfo, mrk)
} else {
m.callCountInfo[mrk] = 0
}
}
// RegisterRegexpMatcherResponder adds a new responder, associated
// with a given HTTP method, URL (or path) regular expression and
// [Matcher].
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// As 2 regexps can match the same URL, the regexp responders are
// tested in the order they are registered. Registering an already
// existing regexp responder (same method, same regexp string and same
// [Matcher] name) replaces its responder, but does not change its
// position, and resets the corresponding statistics as returned by
// [MockTransport.GetCallCountInfo].
//
// If several responders are registered for a same method and urlRegexp
// couple, but with different matchers, they are ordered depending on
// the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [MockTransport.RegisterRegexpResponder]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.GetCallCountInfo]. It does nothing if it does not
// already exist. The original matcher can be passed but also a new
// [Matcher] with the same name and a nil match function as in:
//
// NewMatcher("original matcher name", nil)
//
// A "=~" prefix is added to the stringified regexp in the statistics
// returned by [MockTransport.GetCallCountInfo].
//
// See [MockTransport.RegisterMatcherResponder] function and the "=~"
// prefix in its url parameter to avoid compiling the regexp by
// yourself.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See [MockTransport.RegisterRegexpResponder] if a matcher is not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func (m *MockTransport) RegisterRegexpMatcherResponder(method string, urlRegexp *regexp.Regexp, matcher Matcher, responder Responder) {
m.checkMethod(method, matcher)
m.registerRegexpResponder(regexpResponder{
origRx: regexpPrefix + urlRegexp.String(),
method: method,
rx: urlRegexp,
responders: matchResponders{{matcher: matcher, responder: responder}},
})
}
// RegisterRegexpResponder adds a new responder, associated with a given
// HTTP method and URL (or path) regular expression.
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// As 2 regexps can match the same URL, the regexp responders are
// tested in the order they are registered. Registering an already
// existing regexp responder (same method & same regexp string)
// replaces its responder, but does not change its position, and
// resets the corresponding statistics as returned by
// [MockTransport.GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.MockTransportGetCallCountInfo]. It does nothing if
// it does not already exist.
//
// A "=~" prefix is added to the stringified regexp in the statistics
// returned by [MockTransport.GetCallCountInfo].
//
// See [MockTransport.RegisterResponder] function and the "=~" prefix
// in its url parameter to avoid compiling the regexp by yourself.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See [MockTransport.RegisterRegexpMatcherResponder] to also match on
// request header and/or body.
func (m *MockTransport) RegisterRegexpResponder(method string, urlRegexp *regexp.Regexp, responder Responder) {
m.RegisterRegexpMatcherResponder(method, urlRegexp, Matcher{}, responder)
}
// RegisterMatcherResponderWithQuery is same as
// [MockTransport.RegisterMatcherResponder], but it doesn't depend on
// query items order.
//
// If query is non-nil, its type can be:
//
// - [url.Values]
// - map[string]string
// - string, a query string like "a=12&a=13&b=z&c" (see [url.ParseQuery] function)
//
// If the query type is not recognized or the string cannot be parsed
// using [url.ParseQuery], a panic() occurs.
//
// Unlike [MockTransport.RegisterMatcherResponder], path cannot be
// prefixed by "=~" to say it is a regexp. If it is, a panic occurs.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [MockTransport.GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.GetCallCountInfo]. It does nothing if it does not
// already exist. The original matcher can be passed but also a new
// [Matcher] with the same name and a nil match function as in:
//
// NewMatcher("original matcher name", nil)
//
// If several responders are registered for a same method, path and
// query tuple, but with different matchers, they are ordered
// depending on the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [MockTransport.RegisterResponderWithQuery]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See also [MockTransport.RegisterResponderWithQuery] if a matcher is
// not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func (m *MockTransport) RegisterMatcherResponderWithQuery(method, path string, query any, matcher Matcher, responder Responder) {
if isRegexpURL(path) {
panic(`path begins with "=~", RegisterResponder should be used instead of RegisterResponderWithQuery`)
}
var mapQuery url.Values
switch q := query.(type) {
case url.Values:
mapQuery = q
case map[string]string:
mapQuery = make(url.Values, len(q))
for key, e := range q {
mapQuery[key] = []string{e}
}
case string:
var err error
mapQuery, err = url.ParseQuery(q)
if err != nil {
panic("RegisterResponderWithQuery bad query string: " + err.Error())
}
default:
if query != nil {
panic(fmt.Sprintf("RegisterResponderWithQuery bad query type %T. Only url.Values, map[string]string and string are allowed", query))
}
}
if queryString := sortedQuery(mapQuery); queryString != "" {
path += "?" + queryString
}
m.RegisterMatcherResponder(method, path, matcher, responder)
}
// RegisterResponderWithQuery is same as
// [MockTransport.RegisterResponder], but it doesn't depend on query
// items order.
//
// If query is non-nil, its type can be:
//
// - [url.Values]
// - map[string]string
// - string, a query string like "a=12&a=13&b=z&c" (see [url.ParseQuery] function)
//
// If the query type is not recognized or the string cannot be parsed
// using [url.ParseQuery], a panic() occurs.
//
// Unlike [MockTransport.RegisterResponder], path cannot be prefixed
// by "=~" to say it is a regexp. If it is, a panic occurs.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [MockTransport.GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [MockTransport.GetCallCountInfo]. It does nothing if it does not
// already exist.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See [MockTransport.RegisterMatcherResponderWithQuery] to also match on
// request header and/or body.
func (m *MockTransport) RegisterResponderWithQuery(method, path string, query any, responder Responder) {
m.RegisterMatcherResponderWithQuery(method, path, query, Matcher{}, responder)
}
func sortedQuery(m url.Values) string {
if len(m) == 0 {
return ""
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
var b bytes.Buffer
var values []string // nolint: prealloc
for _, k := range keys {
// Do not alter the passed url.Values
values = append(values, m[k]...)
sort.Strings(values)
k = url.QueryEscape(k)
for _, v := range values {
if b.Len() > 0 {
b.WriteByte('&')
}
fmt.Fprintf(&b, "%v=%v", k, url.QueryEscape(v))
}
values = values[:0]
}
return b.String()
}
// RegisterNoResponder is used to register a responder that is called
// if no other responders are found. The default is [ConnectionFailure]
// that returns a connection error.
//
// Use it in conjunction with [NewNotFoundResponder] to ensure that all
// routes have been mocked:
//
// func TestMyApp(t *testing.T) {
// ...
// // Calls testing.Fatal with the name of Responder-less route and
// // the stack trace of the call.
// mock.RegisterNoResponder(httpmock.NewNotFoundResponder(t.Fatal))
//
// will abort the current test and print something like:
//
// transport_test.go:735: Called from net/http.Get()
// at /go/src/github.com/jarcoal/httpmock/transport_test.go:714
// github.com/jarcoal/httpmock.TestCheckStackTracer()
// at /go/src/testing/testing.go:865
// testing.tRunner()
// at /go/src/runtime/asm_amd64.s:1337
//
// If responder is passed as nil, the default behavior
// ([ConnectionFailure]) is re-enabled.
//
// In some cases you may not want all URLs to be mocked, in which case
// you can do this:
//
// func TestFetchArticles(t *testing.T) {
// ...
// mock.RegisterNoResponder(httpmock.InitialTransport.RoundTrip)
//
// // any requests that don't have a registered URL will be fetched normally
// }
func (m *MockTransport) RegisterNoResponder(responder Responder) {
m.mu.Lock()
m.noResponder = responder
m.mu.Unlock()
}
// Reset removes all registered responders (including the no
// responder) from the [MockTransport]. It zeroes call counters too.
func (m *MockTransport) Reset() {
m.mu.Lock()
m.responders = make(map[internal.RouteKey]matchResponders)
m.regexpResponders = nil
m.noResponder = nil
m.callCountInfo = make(map[matchRouteKey]int)
m.totalCallCount = 0
m.mu.Unlock()
}
// ZeroCallCounters zeroes call counters without touching registered responders.
func (m *MockTransport) ZeroCallCounters() {
m.mu.Lock()
for k := range m.callCountInfo {
m.callCountInfo[k] = 0
}
m.totalCallCount = 0
m.mu.Unlock()
}
// GetCallCountInfo gets the info on all the calls m has caught
// since it was activated or reset. The info is returned as a map of
// the calling keys with the number of calls made to them as their
// value. The key is the method, a space, and the URL all concatenated
// together.
//
// As a special case, regexp responders generate 2 entries for each
// call. One for the call caught and the other for the rule that
// matched. For example:
//
// RegisterResponder("GET", `=~z\.com\z`, NewStringResponder(200, "body"))
// http.Get("http://z.com")
//
// will generate the following result:
//
// map[string]int{
// `GET http://z.com`: 1,
// `GET =~z\.com\z`: 1,
// }
func (m *MockTransport) GetCallCountInfo() map[string]int {
m.mu.RLock()
res := make(map[string]int, len(m.callCountInfo))
for k, v := range m.callCountInfo {
res[k.String()] = v
}
m.mu.RUnlock()
return res
}
// GetTotalCallCount gets the total number of calls m has taken
// since it was activated or reset.
func (m *MockTransport) GetTotalCallCount() int {
m.mu.RLock()
defer m.mu.RUnlock()
return m.totalCallCount
}
// DefaultTransport is the default mock transport used by [Activate],
// [Deactivate], [Reset], [DeactivateAndReset], [RegisterResponder],
// [RegisterRegexpResponder], [RegisterResponderWithQuery] and
// [RegisterNoResponder].
var DefaultTransport = NewMockTransport()
// InitialTransport is a cache of the original transport used so we
// can put it back when [Deactivate] is called.
var InitialTransport = http.DefaultTransport
// oldClients is used to handle custom http clients (i.e clients other
// than http.DefaultClient).
var oldClients = map[*http.Client]http.RoundTripper{}
// oldClientsLock protects oldClients from concurrent writes.
var oldClientsLock sync.Mutex
// Activate starts the mock environment. This should be called before
// your tests run. Under the hood this replaces the [http.Client.Transport]
// field of [http.DefaultClient] with [DefaultTransport].
//
// To enable mocks for a test, simply activate at the beginning of a test:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// // all http requests using http.DefaultTransport will now be intercepted
// }
//
// If you want all of your tests in a package to be mocked, just call
// [Activate] from init():
//
// func init() {
// httpmock.Activate()
// }
//
// or using a TestMain function:
//
// func TestMain(m *testing.M) {
// httpmock.Activate()
// os.Exit(m.Run())
// }
func Activate() {
if Disabled() {
return
}
// make sure that if Activate is called multiple times it doesn't
// overwrite the InitialTransport with a mock transport.
if http.DefaultTransport != DefaultTransport {
InitialTransport = http.DefaultTransport
}
http.DefaultTransport = DefaultTransport
}
// ActivateNonDefault starts the mock environment with a non-default
// [*http.Client]. This emulates the [Activate] function, but allows for
// custom clients that do not use [http.DefaultTransport].
//
// To enable mocks for a test using a custom client, activate at the
// beginning of a test:
//
// client := &http.Client{Transport: &http.Transport{TLSHandshakeTimeout: 60 * time.Second}}
// httpmock.ActivateNonDefault(client)
func ActivateNonDefault(client *http.Client) {
if Disabled() {
return
}
// save the custom client & it's RoundTripper
oldClientsLock.Lock()
defer oldClientsLock.Unlock()
if _, ok := oldClients[client]; !ok {
oldClients[client] = client.Transport
}
client.Transport = DefaultTransport
}
// GetCallCountInfo gets the info on all the calls httpmock has caught
// since it was activated or reset. The info is returned as a map of
// the calling keys with the number of calls made to them as their
// value. The key is the method, a space, and the URL all concatenated
// together.
//
// As a special case, regexp responders generate 2 entries for each
// call. One for the call caught and the other for the rule that
// matched. For example:
//
// RegisterResponder("GET", `=~z\.com\z`, NewStringResponder(200, "body"))
// http.Get("http://z.com")
//
// will generate the following result:
//
// map[string]int{
// `GET http://z.com`: 1,
// `GET =~z\.com\z`: 1,
// }
func GetCallCountInfo() map[string]int {
return DefaultTransport.GetCallCountInfo()
}
// GetTotalCallCount gets the total number of calls httpmock has taken
// since it was activated or reset.
func GetTotalCallCount() int {
return DefaultTransport.GetTotalCallCount()
}
// Deactivate shuts down the mock environment. Any HTTP calls made
// after this will use a live transport.
//
// Usually you'll call it in a defer right after activating the mock
// environment:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// defer httpmock.Deactivate()
//
// // when this test ends, the mock environment will close
// }
//
// Since go 1.14 you can also use [*testing.T.Cleanup] method as in:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// t.Cleanup(httpmock.Deactivate)
//
// // when this test ends, the mock environment will close
// }
//
// useful in test helpers to save your callers from calling defer themselves.
func Deactivate() {
if Disabled() {
return
}
http.DefaultTransport = InitialTransport
// reset the custom clients to use their original RoundTripper
oldClientsLock.Lock()
defer oldClientsLock.Unlock()
for oldClient, oldTransport := range oldClients {
oldClient.Transport = oldTransport
delete(oldClients, oldClient)
}
}
// Reset removes any registered mocks and returns the mock
// environment to its initial state. It zeroes call counters too.
func Reset() {
DefaultTransport.Reset()
}
// ZeroCallCounters zeroes call counters without touching registered responders.
func ZeroCallCounters() {
DefaultTransport.ZeroCallCounters()
}
// DeactivateAndReset is just a convenience method for calling
// [Deactivate] and then [Reset].
//
// Happy deferring!
func DeactivateAndReset() {
Deactivate()
Reset()
}
// RegisterMatcherResponder adds a new responder, associated with a given
// HTTP method, URL (or path) and [Matcher].
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// If url contains query parameters, their order matters as well as
// their content. All following URLs are here considered as different:
//
// http://z.tld?a=1&b=1
// http://z.tld?b=1&a=1
// http://z.tld?a&b
// http://z.tld?a=&b=
//
// If url begins with "=~", the following chars are considered as a
// regular expression. If this regexp can not be compiled, it panics.
// Note that the "=~" prefix remains in statistics returned by
// [GetCallCountInfo]. As 2 regexps can match the same
// URL, the regexp responders are tested in the order they are
// registered. Registering an already existing regexp responder (same
// method & same regexp string) replaces its responder, but does not
// change its position.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by
// [GetCallCountInfo]. It does nothing if it does not
// already exist. The original matcher can be passed but also a new
// [Matcher] with the same name and a nil match function as in:
//
// NewMatcher("original matcher name", nil)
//
// See [RegisterRegexpMatcherResponder] to directly pass a
// [*regexp.Regexp].
//
// Example:
//
// func TestCreateArticle(t *testing.T) {
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
//
// // Mock POST /item only if `"name":"Bob"` is found in request body
// httpmock.RegisterMatcherResponder("POST", "/item",
// httpmock.BodyContainsString(`"name":"Bob"`),
// httpmock.NewStringResponder(201, `{"id":1234}`))
//
// // Can be more acurate with github.com/maxatome/tdhttpmock package
// // paired with github.com/maxatome/go-testdeep/td operators as in
// httpmock.RegisterMatcherResponder("POST", "/item",
// tdhttpmock.JSONBody(td.JSONPointer("/name", "Alice")),
// httpmock.NewStringResponder(201, `{"id":4567}`))
//
// // POST requests to http://anything/item with body containing either
// // `"name":"Bob"` or a JSON message with key "name" set to "Alice"
// // value return the corresponding "id" response
// }
//
// If several responders are registered for a same method and url
// couple, but with different matchers, they are ordered depending on
// the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [RegisterResponder]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See also [RegisterResponder] if a matcher is not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func RegisterMatcherResponder(method, url string, matcher Matcher, responder Responder) {
DefaultTransport.RegisterMatcherResponder(method, url, matcher, responder)
}
// RegisterResponder adds a new responder, associated with a given
// HTTP method and URL (or path).
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// If url contains query parameters, their order matters as well as
// their content. All following URLs are here considered as different:
//
// http://z.tld?a=1&b=1
// http://z.tld?b=1&a=1
// http://z.tld?a&b
// http://z.tld?a=&b=
//
// If url begins with "=~", the following chars are considered as a
// regular expression. If this regexp can not be compiled, it panics.
// Note that the "=~" prefix remains in statistics returned by
// [GetCallCountInfo]. As 2 regexps can match the same URL, the regexp
// responders are tested in the order they are registered. Registering
// an already existing regexp responder (same method & same regexp
// string) replaces its responder, but does not change its position.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by [GetCallCountInfo]. It does
// nothing if it does not already exist.
//
// See [RegisterRegexpResponder] to directly pass a *regexp.Regexp.
//
// Example:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
//
// httpmock.RegisterResponder("GET", "http://example.com/",
// httpmock.NewStringResponder(200, "hello world"))
//
// httpmock.RegisterResponder("GET", "/path/only",
// httpmock.NewStringResponder(200, "any host hello world"))
//
// httpmock.RegisterResponder("GET", `=~^/item/id/\d+\z`,
// httpmock.NewStringResponder(200, "any item get"))
//
// // requests to http://example.com/ now return "hello world" and
// // requests to any host with path /path/only return "any host hello world"
// // requests to any host with path matching ^/item/id/\d+\z regular expression return "any item get"
// }
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting
// [DefaultTransport].DontCheckMethod to true prior to this call.
func RegisterResponder(method, url string, responder Responder) {
DefaultTransport.RegisterResponder(method, url, responder)
}
// RegisterRegexpMatcherResponder adds a new responder, associated
// with a given HTTP method, URL (or path) regular expression and
// [Matcher].
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// As 2 regexps can match the same URL, the regexp responders are
// tested in the order they are registered. Registering an already
// existing regexp responder (same method, same regexp string and same
// [Matcher] name) replaces its responder, but does not change its
// position, and resets the corresponding statistics as returned by
// [GetCallCountInfo].
//
// If several responders are registered for a same method and urlRegexp
// couple, but with different matchers, they are ordered depending on
// the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [RegisterRegexpResponder]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by [GetCallCountInfo]. It does
// nothing if it does not already exist. The original matcher can be
// passed but also a new [Matcher] with the same name and a nil match
// function as in:
//
// NewMatcher("original matcher name", nil)
//
// A "=~" prefix is added to the stringified regexp in the statistics
// returned by [GetCallCountInfo].
//
// See [RegisterMatcherResponder] function and the "=~" prefix in its
// url parameter to avoid compiling the regexp by yourself.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See [RegisterRegexpResponder] if a matcher is not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func RegisterRegexpMatcherResponder(method string, urlRegexp *regexp.Regexp, matcher Matcher, responder Responder) {
DefaultTransport.RegisterRegexpMatcherResponder(method, urlRegexp, matcher, responder)
}
// RegisterRegexpResponder adds a new responder, associated with a given
// HTTP method and URL (or path) regular expression.
//
// When a request comes in that matches, the responder is called and
// the response returned to the client.
//
// As 2 regexps can match the same URL, the regexp responders are
// tested in the order they are registered. Registering an already
// existing regexp responder (same method & same regexp string)
// replaces its responder, but does not change its position, and
// resets the corresponding statistics as returned by [GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by [GetCallCountInfo]. It does
// nothing if it does not already exist.
//
// A "=~" prefix is added to the stringified regexp in the statistics
// returned by [GetCallCountInfo].
//
// See [RegisterResponder] function and the "=~" prefix in its url
// parameter to avoid compiling the regexp by yourself.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting
// DefaultTransport.DontCheckMethod to true prior to this call.
func RegisterRegexpResponder(method string, urlRegexp *regexp.Regexp, responder Responder) {
DefaultTransport.RegisterRegexpResponder(method, urlRegexp, responder)
}
// RegisterMatcherResponderWithQuery is same as
// [RegisterMatcherResponder], but it doesn't depend on query items
// order.
//
// If query is non-nil, its type can be:
//
// - [url.Values]
// - map[string]string
// - string, a query string like "a=12&a=13&b=z&c" (see [url.ParseQuery] function)
//
// If the query type is not recognized or the string cannot be parsed
// using [url.ParseQuery], a panic() occurs.
//
// Unlike [RegisterMatcherResponder], path cannot be prefixed by "=~"
// to say it is a regexp. If it is, a panic occurs.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by [GetCallCountInfo]. It does
// nothing if it does not already exist. The original matcher can be
// passed but also a new [Matcher] with the same name and a nil match
// function as in:
//
// NewMatcher("original matcher name", nil)
//
// If several responders are registered for a same method, path and
// query tuple, but with different matchers, they are ordered
// depending on the following rules:
// - the zero matcher, Matcher{} (or responder set using
// [.RegisterResponderWithQuery]) is always called lastly;
// - other matchers are ordered by their name. If a matcher does not
// have an explicit name ([NewMatcher] called with an empty name and
// [Matcher.WithName] method not called), a name is automatically
// computed so all anonymous matchers are sorted by their creation
// order. An automatically computed name has always the form
// "~HEXANUMBER@PKG.FUNC() FILE:LINE". See [NewMatcher] for details.
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
//
// See also [RegisterResponderWithQuery] if a matcher is not needed.
//
// Note that [github.com/maxatome/tdhttpmock] provides powerful helpers
// to create matchers with the help of [github.com/maxatome/go-testdeep].
func RegisterMatcherResponderWithQuery(method, path string, query any, matcher Matcher, responder Responder) {
DefaultTransport.RegisterMatcherResponderWithQuery(method, path, query, matcher, responder)
}
// RegisterResponderWithQuery it is same as [RegisterResponder], but
// doesn't depends on query items order.
//
// query type can be:
//
// - [url.Values]
// - map[string]string
// - string, a query string like "a=12&a=13&b=z&c" (see [url.ParseQuery] function)
//
// If the query type is not recognized or the string cannot be parsed
// using [url.ParseQuery], a panic() occurs.
//
// Unlike [RegisterResponder], path cannot be prefixed by "=~" to say it
// is a regexp. If it is, a panic occurs.
//
// Registering an already existing responder resets the corresponding
// statistics as returned by [GetCallCountInfo].
//
// Registering a nil [Responder] removes the existing one and the
// corresponding statistics as returned by [GetCallCountInfo]. It does
// nothing if it does not already exist.
//
// Example using a [url.Values]:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
//
// expectedQuery := net.Values{
// "a": []string{"3", "1", "8"},
// "b": []string{"4", "2"},
// }
// httpmock.RegisterResponderWithQueryValues(
// "GET", "http://example.com/", expectedQuery,
// httpmock.NewStringResponder(200, "hello world"))
//
// // requests to http://example.com?a=1&a=3&a=8&b=2&b=4
// // and to http://example.com?b=4&a=2&b=2&a=8&a=1
// // now return 'hello world'
// }
//
// or using a map[string]string:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
//
// expectedQuery := map[string]string{
// "a": "1",
// "b": "2"
// }
// httpmock.RegisterResponderWithQuery(
// "GET", "http://example.com/", expectedQuery,
// httpmock.NewStringResponder(200, "hello world"))
//
// // requests to http://example.com?a=1&b=2 and http://example.com?b=2&a=1 now return 'hello world'
// }
//
// or using a query string:
//
// func TestFetchArticles(t *testing.T) {
// httpmock.Activate()
// defer httpmock.DeactivateAndReset()
//
// expectedQuery := "a=3&b=4&b=2&a=1&a=8"
// httpmock.RegisterResponderWithQueryValues(
// "GET", "http://example.com/", expectedQuery,
// httpmock.NewStringResponder(200, "hello world"))
//
// // requests to http://example.com?a=1&a=3&a=8&b=2&b=4
// // and to http://example.com?b=4&a=2&b=2&a=8&a=1
// // now return 'hello world'
// }
//
// If method is a lower-cased version of CONNECT, DELETE, GET, HEAD,
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting
// DefaultTransport.DontCheckMethod to true prior to this call.
func RegisterResponderWithQuery(method, path string, query any, responder Responder) {
RegisterMatcherResponderWithQuery(method, path, query, Matcher{}, responder)
}
// RegisterNoResponder is used to register a responder that is called
// if no other responders are found. The default is [ConnectionFailure]
// that returns a connection error.
//
// Use it in conjunction with [NewNotFoundResponder] to ensure that all
// routes have been mocked:
//
// import (
// "testing"
// "github.com/jarcoal/httpmock"
// )
// ...
// func TestMyApp(t *testing.T) {
// ...
// // Calls testing.Fatal with the name of Responder-less route and
// // the stack trace of the call.
// httpmock.RegisterNoResponder(httpmock.NewNotFoundResponder(t.Fatal))
//
// will abort the current test and print something like:
//
// transport_test.go:735: Called from net/http.Get()
// at /go/src/github.com/jarcoal/httpmock/transport_test.go:714
// github.com/jarcoal/httpmock.TestCheckStackTracer()
// at /go/src/testing/testing.go:865
// testing.tRunner()
// at /go/src/runtime/asm_amd64.s:1337
//
// If responder is passed as nil, the default behavior
// ([ConnectionFailure]) is re-enabled.
//
// In some cases you may not want all URLs to be mocked, in which case
// you can do this:
//
// func TestFetchArticles(t *testing.T) {
// ...
// httpmock.RegisterNoResponder(httpmock.InitialTransport.RoundTrip)
//
// // any requests that don't have a registered URL will be fetched normally
// }
func RegisterNoResponder(responder Responder) {
DefaultTransport.RegisterNoResponder(responder)
}
// ErrSubmatchNotFound is the error returned by GetSubmatch* functions
// when the given submatch index cannot be found.
var ErrSubmatchNotFound = errors.New("submatch not found")
// GetSubmatch has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as a string. Example:
//
// RegisterResponder("GET", `=~^/item/name/([^/]+)\z`,
// func(req *http.Request) (*http.Response, error) {
// name, err := GetSubmatch(req, 1) // 1=first regexp submatch
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]any{
// "id": 123,
// "name": name,
// })
// })
//
// It panics if n < 1. See [MustGetSubmatch] to avoid testing the
// returned error.
func GetSubmatch(req *http.Request, n int) (string, error) {
if n <= 0 {
panic(fmt.Sprintf("getting submatches starts at 1, not %d", n))
}
n--
submatches := internal.GetSubmatches(req)
if n >= len(submatches) {
return "", ErrSubmatchNotFound
}
return submatches[n], nil
}
// GetSubmatchAsInt has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as an int64. Example:
//
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id, err := GetSubmatchAsInt(req, 1) // 1=first regexp submatch
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
// })
//
// It panics if n < 1. See [MustGetSubmatchAsInt] to avoid testing the
// returned error.
func GetSubmatchAsInt(req *http.Request, n int) (int64, error) {
sm, err := GetSubmatch(req, n)
if err != nil {
return 0, err
}
return strconv.ParseInt(sm, 10, 64)
}
// GetSubmatchAsUint has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as a uint64. Example:
//
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id, err := GetSubmatchAsUint(req, 1) // 1=first regexp submatch
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
// })
//
// It panics if n < 1. See [MustGetSubmatchAsUint] to avoid testing the
// returned error.
func GetSubmatchAsUint(req *http.Request, n int) (uint64, error) {
sm, err := GetSubmatch(req, n)
if err != nil {
return 0, err
}
return strconv.ParseUint(sm, 10, 64)
}
// GetSubmatchAsFloat has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as a float64. Example:
//
// RegisterResponder("PATCH", `=~^/item/id/\d+\?height=(\d+(?:\.\d*)?)\z`,
// func(req *http.Request) (*http.Response, error) {
// height, err := GetSubmatchAsFloat(req, 1) // 1=first regexp submatch
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// "height": height,
// })
// })
//
// It panics if n < 1. See [MustGetSubmatchAsFloat] to avoid testing the
// returned error.
func GetSubmatchAsFloat(req *http.Request, n int) (float64, error) {
sm, err := GetSubmatch(req, n)
if err != nil {
return 0, err
}
return strconv.ParseFloat(sm, 64)
}
// MustGetSubmatch works as [GetSubmatch] except that it panics in
// case of error (submatch not found). It has to be used in Responders
// installed by [RegisterRegexpResponder] or [RegisterResponder] +
// "=~" URL prefix (as well as [MockTransport.RegisterRegexpResponder]
// or [MockTransport.RegisterResponder]). It allows to retrieve the
// n-th submatch of the matching regexp, as a string. Example:
//
// RegisterResponder("GET", `=~^/item/name/([^/]+)\z`,
// func(req *http.Request) (*http.Response, error) {
// name := MustGetSubmatch(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]any{
// "id": 123,
// "name": name,
// })
// })
//
// It panics if n < 1.
func MustGetSubmatch(req *http.Request, n int) string {
s, err := GetSubmatch(req, n)
if err != nil {
panic("GetSubmatch failed: " + err.Error())
}
return s
}
// MustGetSubmatchAsInt works as [GetSubmatchAsInt] except that it
// panics in case of error (submatch not found or invalid int64
// format). It has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as an int64. Example:
//
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id := MustGetSubmatchAsInt(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
// })
//
// It panics if n < 1.
func MustGetSubmatchAsInt(req *http.Request, n int) int64 {
i, err := GetSubmatchAsInt(req, n)
if err != nil {
panic("GetSubmatchAsInt failed: " + err.Error())
}
return i
}
// MustGetSubmatchAsUint works as [GetSubmatchAsUint] except that it
// panics in case of error (submatch not found or invalid uint64
// format). It has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as a uint64. Example:
//
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id, err := MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
// })
//
// It panics if n < 1.
func MustGetSubmatchAsUint(req *http.Request, n int) uint64 {
u, err := GetSubmatchAsUint(req, n)
if err != nil {
panic("GetSubmatchAsUint failed: " + err.Error())
}
return u
}
// MustGetSubmatchAsFloat works as [GetSubmatchAsFloat] except that it
// panics in case of error (submatch not found or invalid float64
// format). It has to be used in Responders installed by
// [RegisterRegexpResponder] or [RegisterResponder] + "=~" URL prefix
// (as well as [MockTransport.RegisterRegexpResponder] or
// [MockTransport.RegisterResponder]). It allows to retrieve the n-th
// submatch of the matching regexp, as a float64. Example:
//
// RegisterResponder("PATCH", `=~^/item/id/\d+\?height=(\d+(?:\.\d*)?)\z`,
// func(req *http.Request) (*http.Response, error) {
// height := MustGetSubmatchAsFloat(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// "height": height,
// })
// })
//
// It panics if n < 1.
func MustGetSubmatchAsFloat(req *http.Request, n int) float64 {
f, err := GetSubmatchAsFloat(req, n)
if err != nil {
panic("GetSubmatchAsFloat failed: " + err.Error())
}
return f
}
|