Skip to content

Instantly share code, notes, and snippets.

@iheanyi
Created January 14, 2017 21:10
Show Gist options
  • Save iheanyi/85baf30c8e94d8b9690ad682a1dc4ee6 to your computer and use it in GitHub Desktop.
Save iheanyi/85baf30c8e94d8b9690ad682a1dc4ee6 to your computer and use it in GitHub Desktop.
Simple Mock OAuth Test for SPA + API Go application
import (
headspin "github.com/iheanyi/headspin-backend"
"github.com/labstack/echo"
"golang.org/x/oauth2"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func setupMockOAuthServer() (*httptest.Server, func()) {
mux := http.NewServeMux()
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
// Should return authorization code back to the user
})
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
// Should return acccess token back to the user
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
w.Write([]byte("access_token=mocktoken&scope=user&token_type=bearer"))
})
server := httptest.NewServer(mux)
return server, func() {
server.Close()
}
}
func newOAuthConf(url string) *oauth2.Config {
return &oauth2.Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: []string{"email", "profile"},
Endpoint: oauth2.Endpoint{
AuthURL: url + "/auth",
TokenURL: url + "/token",
},
}
}
func TestMockOAuth(t *testing.T) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment