]> git.giacomolli.uk - onechat/blobdiff - chatgpt_fake_helper_test.go
Resume queued appends at their saved log ID
[onechat] / chatgpt_fake_helper_test.go
index dfcf17d82cc504a9e2cfc9b4ac837d0b9a65b9b2..ddd4c6736861643642d5e50a8861bcb24a13c0b1 100644 (file)
@@ -67,9 +67,11 @@ func (f *ChatGPTFake) newToken(ttl time.Duration) string {
 
 func (f *ChatGPTFake) userCode(w http.ResponseWriter, r *http.Request) {
        var body map[string]string
+
        err := json.NewDecoder(r.Body).Decode(&body)
        if err != nil || body["client_id"] != chatgptClientID {
                http.Error(w, "bad client", http.StatusBadRequest)
+
                return
        }
 
@@ -85,20 +87,24 @@ func (f *ChatGPTFake) deviceToken(w http.ResponseWriter, r *http.Request) {
        defer f.mutex.Unlock()
 
        var body map[string]string
+
        err := json.NewDecoder(r.Body).Decode(&body)
        if err != nil || body["device_auth_id"] != "device-1" || body["user_code"] != "CODE-1234" {
                http.Error(w, "bad device", http.StatusBadRequest)
+
                return
        }
 
        f.polls++
        if f.deny {
                http.Error(w, "denied", http.StatusBadRequest)
+
                return
        }
 
        if f.polls <= f.pending {
                http.Error(w, "pending", http.StatusForbidden)
+
                return
        }
 
@@ -117,6 +123,7 @@ func (f *ChatGPTFake) oauthToken(w http.ResponseWriter, r *http.Request) {
                err := r.ParseForm()
                if err != nil {
                        http.Error(w, err.Error(), http.StatusBadRequest)
+
                        return
                }
 
@@ -131,9 +138,11 @@ func (f *ChatGPTFake) oauthToken(w http.ResponseWriter, r *http.Request) {
        }
 
        var body map[string]string
+
        err := json.NewDecoder(r.Body).Decode(&body)
        if err != nil || body["grant_type"] != "refresh_token" || body["client_id"] != chatgptClientID {
                http.Error(w, "bad refresh", http.StatusBadRequest)
+
                return
        }
 
@@ -141,6 +150,7 @@ func (f *ChatGPTFake) oauthToken(w http.ResponseWriter, r *http.Request) {
        f.refreshed = body["refresh_token"]
        if f.failRefresh {
                http.Error(w, `{"error": "invalid_grant", "token": "`+body["refresh_token"]+`"}`, http.StatusUnauthorized)
+
                return
        }
 
@@ -164,6 +174,7 @@ func newChatGPTFake(t *testing.T) *ChatGPTFake {
        mux.HandleFunc("POST /api/accounts/deviceauth/usercode", f.userCode)
        mux.HandleFunc("POST /api/accounts/deviceauth/token", f.deviceToken)
        mux.HandleFunc("POST /oauth/token", f.oauthToken)
+
        f.Server = httptest.NewServer(mux)
        t.Cleanup(f.Close)
 
@@ -190,6 +201,7 @@ func chatgptWriteTestTokens(t *testing.T, auth *ChatGPTAuth, access, refresh str
 
 func chatgptWriteJSON(w http.ResponseWriter, value any) {
        w.Header().Set("Content-Type", "application/json")
+
        err := json.NewEncoder(w).Encode(value)
        if err != nil {
                panic(fmt.Sprint("encode answer: ", err))