Test ortamında OTPMOCK_URL=https://api.otpmock.com tanımlayın, Plivo istemcisini bu adrese yönlendirin ve Plivo kimlik bilgisi olarak otpmock API anahtarınızı kullanın. Uygulama kodunuz değişmez; canlı ortam Plivo ile göndermeye devam eder.
Kurulum
Plivo SDK'sı, hesap temel adresini değiştiren bir url seçeneği kabul eder. Çalışır, ama Plivo bunu belgelemiyor.
import plivo from "plivo";
const authId = process.env.PLIVO_AUTH_ID;
export const plivoClient = new plivo.Client(
authId,
process.env.PLIVO_AUTH_TOKEN, // testte: otpmock API anahtarı
process.env.OTPMOCK_URL ? { url: `${process.env.OTPMOCK_URL}/v1/Account/${authId}` } : {},
);import os
from urllib.parse import urlparse
import plivo
plivo_client = plivo.RestClient(
os.environ["PLIVO_AUTH_ID"],
os.environ["PLIVO_AUTH_TOKEN"], # testte: otpmock API anahtarı
)
otpmock = os.environ.get("OTPMOCK_URL")
if otpmock: # the SDK has no host option: rewrite the host of each request
_send = plivo_client.session.send
def _send_to_otpmock(request, *args, **kwargs):
u = urlparse(request.url)
request.url = otpmock.rstrip("/") + u.path + ("?" + u.query if u.query else "")
return _send(request, *args, **kwargs)
plivo_client.session.send = _send_to_otpmockimport com.fasterxml.jackson.databind.module.SimpleModule;
import com.plivo.api.PlivoClient;
import com.plivo.api.models.base.LogLevel;
import okhttp3.OkHttpClient;
String otpmock = System.getenv("OTPMOCK_URL");
String authToken = System.getenv("PLIVO_AUTH_TOKEN"); // testte: otpmock API anahtarı
PlivoClient plivo = otpmock == null
? new PlivoClient(authId, authToken)
: new PlivoClient(authId, authToken, new OkHttpClient.Builder(), otpmock + "/v1/", new SimpleModule(), LogLevel.NONE);
// Message.creator(src, dst, text).client(plivo).create();
// VerifySession.creator(...).client(plivo).create();// The Plivo .NET SDK has no base URL option; in apps that must be testable, call the REST API.
var plivoUrl = Environment.GetEnvironmentVariable("OTPMOCK_URL") ?? "https://api.plivo.com";
var authId = Environment.GetEnvironmentVariable("PLIVO_AUTH_ID");
var authToken = Environment.GetEnvironmentVariable("PLIVO_AUTH_TOKEN"); // testte: otpmock API anahtarı
var http = new HttpClient { BaseAddress = new Uri(plivoUrl) };
http.DefaultRequestHeaders.Authorization = new("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{authId}:{authToken}")));
await http.PostAsJsonAsync($"/v1/Account/{authId}/Message/",
new { src = "+15550000001", dst = phone, text = $"Your code is {code}" });use GuzzleHttp\Client;
// plivo/plivo-php hard-codes api.plivo.com, so call the REST path directly.
$plivoUrl = getenv('OTPMOCK_URL') ?: 'https://api.plivo.com';
$authId = getenv('PLIVO_AUTH_ID');
(new Client())->post("$plivoUrl/v1/Account/$authId/Message/", [
'auth' => [$authId, getenv('PLIVO_AUTH_TOKEN')], // auth token = testte: otpmock API anahtarı
'json' => ['src' => '+15550000001', 'dst' => '+15550142', 'text' => "Your code is $code"],
]);require "net/http"
require "json"
PLIVO_URL = ENV.fetch("OTPMOCK_URL", "https://api.plivo.com")
auth_id = ENV["PLIVO_AUTH_ID"]
uri = URI("#{PLIVO_URL}/v1/Account/#{auth_id}/Message/")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req.basic_auth(auth_id, ENV["PLIVO_AUTH_TOKEN"]) # testte: otpmock API anahtarı
req.body = { src: "+15550000001", dst: "+15550142", text: "Your code is #{code}" }.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }// otpmockTransport sends SDK requests to otpmock, keeping path, query and auth.
type otpmockTransport struct{ base *url.URL }
func (t otpmockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r = r.Clone(r.Context())
r.URL.Scheme, r.URL.Host, r.Host = t.base.Scheme, t.base.Host, t.base.Host
return http.DefaultTransport.RoundTrip(r)
}
func newPlivo() (*plivo.Client, error) {
opts := &plivo.ClientOptions{}
if base, err := url.Parse(os.Getenv("OTPMOCK_URL")); err == nil && base.Host != "" {
opts.HttpClient = &http.Client{Transport: otpmockTransport{base}}
}
return plivo.NewClient(os.Getenv("PLIVO_AUTH_ID"), os.Getenv("PLIVO_AUTH_TOKEN"), opts) // token = testte: otpmock API anahtarı
}Her dil, CI'da sağlayıcının resmi SDK'sıyla (ya da başka bir adrese yönlendirilemeyen SDK'larda düz HTTP API'siyle) otpmock'a karşı sınanır.
Kimlik bilgisi. Test ortamında PLIVO_AUTH_TOKEN değerini otpmock API anahtarınıza çevirin. Auth ID olduğu gibi kalabilir.
otpmock neyi taklit ediyor?
| Uç nokta | Ne yapar |
|---|---|
POST /v1/Account/{auth_id}/Message/ | SMS gönderir (birden çok alıcı < ile ayrılır) |
POST /v1/Account/{auth_id}/Verify/Session/ | Verify: oturum oluşturur (OTP'yi otpmock üretir; code_length 4–8, varsayılan 6; metinde brand_name kullanılır) |
POST /v1/Account/{auth_id}/Verify/Session/{uuid}/ | Verify: otp değerini doğrular (yanlış OTP'de 400, en çok 10 deneme) |
GET /v1/Account/{auth_id}/Verify/Session/{uuid}/ | Verify: oturumu getirir (durum in-progress, verified ya da expired) |
Yanıtlar ve hatalar Plivo API'sinin kendi biçimindedir, bu yüzden istemci kütüphanesi onları her zamanki gibi çözümler. Yanlış anahtar ve aylık hakkın dolması da aynı hata biçimiyle döner.
Telefon numaraları
E.164 biçiminde (+15550142) ya da + olmadan gönderin.
Testinizde kodu okuyun
const telefon = otp.randomPhone(); // her test için ayrı numara
const since = Date.now() - 5_000;
await page.getByRole("button", { name: "Kod gönder" }).click(); // uygulamanız Plivo API'sini çağırır
const { code } = await otp.waitForCode(telefon, { since });otp yardımcısı bağımlılığı olmayan tek bir dosyadır: dokümandan alın (İngilizce). Baştan sona örnekler: Playwright ve Cypress rehberleri (İngilizce).
Sık sorulanlar
otpmock Plivo üzerinden gerçek SMS gönderiyor mu?
Hayır. Test ortamında uygulamanızın Plivo istekleri otpmock'a gider; mesaj 10 dakika saklanır ve Plivo ya da herhangi bir operatör ağına hiç ulaşmaz. Canlı ortamınız Plivo kullanmaya devam eder, çünkü otpmock yalnızca OTPMOCK_URL tanımlıyken devreye girer.
Resmi plivo SDK'sı destekleniyor mu?
Evet. Plivo uç noktaları, resmi plivo paketiyle Node.js'te uçtan uca sınanır. Adres değiştirmeye izin veren başka dillerdeki istemciler de çalışır.
otpmock hangi Plivo API'lerini destekliyor?
POST /v1/Account/{auth_id}/Message/ (SMS gönderir (birden çok alıcı < ile ayrılır)); POST /v1/Account/{auth_id}/Verify/Session/ (Verify: oturum oluşturur (OTP'yi otpmock üretir; code_length 4–8, varsayılan 6; metinde brand_name kullanılır)); POST /v1/Account/{auth_id}/Verify/Session/{uuid}/ (Verify: otp değerini doğrular (yanlış OTP'de 400, en çok 10 deneme)); GET /v1/Account/{auth_id}/Verify/Session/{uuid}/ (Verify: oturumu getirir (durum in-progress, verified ya da expired)).
Ücretsiz plan ayda 300 mesaj içerir, kart gerekmez. Yapay zeka kodlama asistanı mı kullanıyorsunuz? otpmock.com/llms-full.txt adresini verin.
Ücretsiz API anahtarı al