func (m *MockServer) GetLastRequestFor(pattern string, method string) (string, bool) {
	for i := len(m.FullRequests) - 1; i >= 0; i-- {
		r := m.FullRequests[i]
		if r.method == "" || r.method == method {
			if r.pattern == pattern {
				return r.body, true
			}
		}
	}
	return "", false
}
GetLastRequestFor returns the last request for the specified pattern/method.
If method is empty, the response will be applied for any method
package testserver
import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"regexp"
	"strings"
	"testing"
)
func New(t *testing.T, name string) *MockServer {
	mux := http.NewServeMux()
	t.Logf("%s: new server created", name)
	return &MockServer{
		t:                 t,
		name:              name,
		mux:               mux,
		responsesByMethod: make(map[string]map[string]response),
		defaultResponses:  []defaultResponse{},
	}
}
type response struct {
	code    int
	payload string
}
type defaultResponse struct {
	response
	method string
	re     *regexp.Regexp
}
type simpleRequest struct {
	pattern string
	method  string
	body    string
}
type MockServer struct {
	t            *testing.T
	mux          *http.ServeMux
	name         string
	Requests     string
	FullRequests []simpleRequest
	server       *httptest.Server
	errorCode    int
	responsesByMethod map[string]map[string]response
	defaultResponses  []defaultResponse
}
func (m *MockServer) Endpoint() string {
	if m == nil || m.server == nil {
		return "https://ironic.test/v1/"
	}
	response := m.server.URL + "/v1/"
	m.t.Logf("%s: endpoint: %s", m.name, response)
	return response
}
func (m *MockServer) logRequest(r *http.Request, response string) {
	m.t.Logf("%s: %s %s -> %s", m.name, r.Method, r.URL, response)
	m.Requests += r.URL.Path + ";"
	bodyRaw, _ := io.ReadAll(r.Body)
	m.FullRequests = append(m.FullRequests, simpleRequest{
		pattern: r.URL.Path,
		method:  r.Method,
		body:    string(bodyRaw),
	})
}
func (m *MockServer) Handler(pattern string, handlerFunc http.HandlerFunc) *MockServer {
	m.t.Logf("%s: adding handler for %s", m.name, pattern)
	m.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
		handlerFunc(w, r)
	})
	return m
}
func (m *MockServer) buildHandler(pattern string) func(http.ResponseWriter, *http.Request) {
	handler := func(w http.ResponseWriter, r *http.Request) {
		if response, ok := m.responsesByMethod[r.URL.Path][r.Method]; ok {
			m.sendData(w, r, response.code, response.payload)
			return
		}
		m.defaultHandler(w, r)
	}
	return handler
}
func (m *MockServer) parsePattern(patternWithMethod string) (pattern string, method string) {
	method = http.MethodGet
	res := strings.Split(patternWithMethod, ":")
	if len(res) > 1 {
		method = res[1]
	}
	pattern = res[0]
	return
}
func (m *MockServer) ResponseWithCode(patternWithMethod string, payload string, code int) *MockServer {
	pattern, method := m.parsePattern(patternWithMethod)
	mh, ok := m.responsesByMethod[pattern]
	if !ok {
		m.responsesByMethod[pattern] = map[string]response{}
		m.mux.HandleFunc(pattern, m.buildHandler(pattern))
	}
	if _, ok = mh[method]; ok {
		panic(fmt.Sprintf("Method handler for [%s] %s was already defined", method, pattern))
	}
	m.t.Logf("%s: adding response for [%s] %s", m.name, method, pattern)
	m.responsesByMethod[pattern][method] = response{
		code:    code,
		payload: payload,
	}
	return m
}
func (m *MockServer) ResponseJSON(pattern string, payload interface{}) *MockServer {
	content, err := json.Marshal(payload)
	if err != nil {
		m.t.Error(err)
	}
	m.ResponseWithCode(pattern, string(content), http.StatusOK)
	return m
}
func (m *MockServer) ErrorResponse(pattern string, errorCode int) *MockServer {
	m.t.Logf("%s: adding error response handler for %s", m.name, pattern)
	m.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
		m.logRequest(r, fmt.Sprintf("%d", errorCode))
		http.Error(w, "An error", errorCode)
	})
	return m
}
func (m *MockServer) Start() *MockServer {
	m.server = httptest.NewServer(m.mux)
	m.mux.HandleFunc("/", m.defaultHandler)
	return m
}
func (m *MockServer) Stop() {
	m.server.Close()
}
func (m *MockServer) AddDefaultResponseJSON(patternWithVars string, httpMethod string, code int, payload interface{}) *MockServer {
	content, err := json.Marshal(payload)
	if err != nil {
		m.t.Error(err)
	}
	return m.AddDefaultResponse(patternWithVars, httpMethod, code, string(content))
}