github.com/mailgun/mailgun-go/v3@v3.6.4/mock_routes.go (about)

     1  package mailgun
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/go-chi/chi"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  type routeResponse struct {
    11  	Route Route `json:"route"`
    12  }
    13  
    14  func (ms *MockServer) addRoutes(r chi.Router) {
    15  	r.Post("/routes", ms.createRoute)
    16  	r.Get("/routes", ms.listRoutes)
    17  	r.Get("/routes/{id}", ms.getRoute)
    18  	r.Put("/routes/{id}", ms.updateRoute)
    19  	r.Delete("/routes/{id}", ms.deleteRoute)
    20  
    21  	for i := 0; i < 10; i++ {
    22  		ms.routeList = append(ms.routeList, Route{
    23  			Id:          randomString(10, "ID-"),
    24  			Priority:    0,
    25  			Description: fmt.Sprintf("Sample Route %d", i),
    26  			Actions: []string{
    27  				`forward("http://myhost.com/messages/")`,
    28  				`stop()`,
    29  			},
    30  			Expression: `match_recipient(".*@samples.mailgun.org")`,
    31  		})
    32  	}
    33  }
    34  
    35  func (ms *MockServer) listRoutes(w http.ResponseWriter, r *http.Request) {
    36  	skip := stringToInt(r.FormValue("skip"))
    37  	limit := stringToInt(r.FormValue("limit"))
    38  	if limit == 0 {
    39  		limit = 100
    40  	}
    41  
    42  	if skip > len(ms.routeList) {
    43  		skip = len(ms.routeList)
    44  	}
    45  
    46  	end := limit + skip
    47  	if end > len(ms.routeList) {
    48  		end = len(ms.routeList)
    49  	}
    50  
    51  	// If we are at the end of the list
    52  	if skip == end {
    53  		toJSON(w, routesListResponse{
    54  			TotalCount: len(ms.routeList),
    55  			Items:      []Route{},
    56  		})
    57  		return
    58  	}
    59  
    60  	toJSON(w, routesListResponse{
    61  		TotalCount: len(ms.routeList),
    62  		Items:      ms.routeList[skip:end],
    63  	})
    64  }
    65  
    66  func (ms *MockServer) getRoute(w http.ResponseWriter, r *http.Request) {
    67  	for _, item := range ms.routeList {
    68  		if item.Id == chi.URLParam(r, "id") {
    69  			toJSON(w, routeResponse{Route: item})
    70  			return
    71  		}
    72  	}
    73  	w.WriteHeader(http.StatusNotFound)
    74  	toJSON(w, okResp{Message: "route not found"})
    75  }
    76  
    77  func (ms *MockServer) createRoute(w http.ResponseWriter, r *http.Request) {
    78  	if r.FormValue("action") == "" {
    79  		w.WriteHeader(http.StatusBadRequest)
    80  		toJSON(w, okResp{Message: "'action' parameter is required"})
    81  		return
    82  	}
    83  
    84  	ms.routeList = append(ms.routeList, Route{
    85  		CreatedAt:   RFC2822Time(time.Now().UTC()),
    86  		Id:          randomString(10, "ID-"),
    87  		Priority:    stringToInt(r.FormValue("priority")),
    88  		Description: r.FormValue("description"),
    89  		Expression:  r.FormValue("expression"),
    90  		Actions:     r.Form["action"],
    91  	})
    92  	toJSON(w, createRouteResp{
    93  		Message: "Route has been created",
    94  		Route:   ms.routeList[len(ms.routeList)-1],
    95  	})
    96  }
    97  
    98  func (ms *MockServer) updateRoute(w http.ResponseWriter, r *http.Request) {
    99  	for i, item := range ms.routeList {
   100  		if item.Id == chi.URLParam(r, "id") {
   101  
   102  			if r.FormValue("action") != "" {
   103  				ms.routeList[i].Actions = r.Form["action"]
   104  			}
   105  			if r.FormValue("priority") != "" {
   106  				ms.routeList[i].Priority = stringToInt(r.FormValue("priority"))
   107  			}
   108  			if r.FormValue("description") != "" {
   109  				ms.routeList[i].Description = r.FormValue("description")
   110  			}
   111  			if r.FormValue("expression") != "" {
   112  				ms.routeList[i].Expression = r.FormValue("expression")
   113  			}
   114  			toJSON(w, ms.routeList[i])
   115  			return
   116  		}
   117  	}
   118  	w.WriteHeader(http.StatusNotFound)
   119  	toJSON(w, okResp{Message: "route not found"})
   120  }
   121  
   122  func (ms *MockServer) deleteRoute(w http.ResponseWriter, r *http.Request) {
   123  	result := ms.routeList[:0]
   124  	for _, item := range ms.routeList {
   125  		if item.Id == chi.URLParam(r, "id") {
   126  			continue
   127  		}
   128  		result = append(result, item)
   129  	}
   130  
   131  	if len(result) != len(ms.domainList) {
   132  		toJSON(w, okResp{Message: "success"})
   133  		ms.routeList = result
   134  		return
   135  	}
   136  
   137  	w.WriteHeader(http.StatusNotFound)
   138  	toJSON(w, okResp{Message: "route not found"})
   139  }