-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package mock | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/kayac/Gunfish/fcmv1" | ||
) | ||
|
||
func FCMv1MockServer(projectID string, verbose bool) *http.ServeMux { | ||
mux := http.NewServeMux() | ||
p := fmt.Sprintf("/v1/projects/%s/messages:send", projectID) | ||
log.Println("fcmv1 mock server path:", p) | ||
mux.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) { | ||
start := time.Now() | ||
defer func() { | ||
if verbose { | ||
log.Printf("reqtime:%f proto:%s method:%s path:%s host:%s", reqtime(start), r.Proto, r.Method, r.URL.Path, r.RemoteAddr) | ||
} | ||
}() | ||
|
||
// sets the response time from FCM server | ||
time.Sleep(time.Millisecond*200 + time.Millisecond*(time.Duration(rand.Int63n(200)-100))) | ||
token := r.Header.Get("Authorization") | ||
token = strings.TrimPrefix(token, "Bearer ") | ||
|
||
w.Header().Set("Content-Type", ApplicationJSON) | ||
switch token { | ||
case fcmv1.InvalidArgument: | ||
createFCMv1ErrorResponse(w, http.StatusBadRequest, fcmv1.InvalidArgument) | ||
case fcmv1.Unregistered: | ||
createFCMv1ErrorResponse(w, http.StatusNotFound, fcmv1.Unregistered) | ||
case fcmv1.Unavailable: | ||
createFCMv1ErrorResponse(w, http.StatusServiceUnavailable, fcmv1.Unavailable) | ||
case fcmv1.Internal: | ||
createFCMv1ErrorResponse(w, http.StatusInternalServerError, fcmv1.Internal) | ||
case fcmv1.QuotaExceeded: | ||
createFCMv1ErrorResponse(w, http.StatusTooManyRequests, fcmv1.QuotaExceeded) | ||
default: | ||
enc := json.NewEncoder(w) | ||
enc.Encode(fcmv1.ResponseBody{ | ||
Name: "ok", | ||
}) | ||
} | ||
}) | ||
|
||
return mux | ||
} | ||
|
||
func createFCMv1ErrorResponse(w http.ResponseWriter, code int, status string) error { | ||
w.WriteHeader(code) | ||
enc := json.NewEncoder(w) | ||
return enc.Encode(fcmv1.ResponseBody{ | ||
Error: &fcmv1.FCMError{ | ||
Status: status, | ||
Message: "mock error:" + status, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/kayac/Gunfish/mock" | ||
) | ||
|
||
func main() { | ||
var ( | ||
port int | ||
projectID string | ||
verbose bool | ||
) | ||
|
||
flag.IntVar(&port, "port", 8888, "fcmv1 mock server port") | ||
flag.StringVar(&projectID, "project-id", "test", "fcmv1 mock project id") | ||
flag.BoolVar(&verbose, "verbose", false, "verbose flag") | ||
flag.Parse() | ||
|
||
mux := mock.FCMv1MockServer(projectID, verbose) | ||
log.Println("start fcmv1mock server port:", port, "project_id:", projectID) | ||
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), mux); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |