首页 \ 问答 \ 协议:如何检查可能存在或不存在的响应中的字段(pact: how to check a a field in the response that may or may not exist)

协议:如何检查可能存在或不存在的响应中的字段(pact: how to check a a field in the response that may or may not exist)

我有一个服务可以用json blob响应请求,例如:

{
  "field1": 1,
  "field2": "2",
  "array": [1,2,3]
}

我知道我可以使用EachLike测试array ,如下所示:

expected = {
  "field1": Like(1),
  "field2": Like("2"),
  "array": EachLike(1)
}

问题在于“数组”是响应中的一个可选字段。 它可能根本不存在,如果不存在,我仍然需要合同来验证。 如何定义响应主体中的字段必须与某个类型匹配(如果存在),但它可能根本不存在?


I have a service that will respond to a request with a json blob, such as this:

{
  "field1": 1,
  "field2": "2",
  "array": [1,2,3]
}

I know that I can test array by using EachLike, like this:

expected = {
  "field1": Like(1),
  "field2": Like("2"),
  "array": EachLike(1)
}

The issue is that "array" is an optional field in the response. It may not exist at all, and if it doesn't, I still need the contract to validate. How do I define that a field in the response body must match a type if it exists, but that it may not exist at all?


原文:https://stackoverflow.com/questions/47595272
更新时间:2023-06-12 11:06

最满意答案

在我在我的问题中提到的应用程序中,没有覆盖默认行为。 这是我的错误,我采取了错误的方式。 使用Handler可以实现相同的功能。 在我的应用程序中,我使用了一个单击按钮启动的服务 。 在服务内部,我曾经使用过处理程序。 下面,我发布了代码片段。

public class DialerService extends Service {
ActivityManager am;
List<RunningAppProcessInfo> mAppProcessInfosList;
private Runnable myRunnable;
boolean threadDone = true;
Handler mHandler;
boolean isLockedAppRunning = false;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

public void onCreate() {
    am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    mAppProcessInfosList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
    mHandler = new Handler();
    Log.v("Dialer Service", "onCreate called");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    myRunnable = new Runnable() {

        @Override
        public void run() {
            isRestrictedAppRunning();
        }
    };

    new Thread(new Runnable() {
        public void run() {
            while (threadDone) {

                try {
                    mHandler.post(myRunnable);
                } catch (Exception e) {

                }
            }
        }
    }).start();
    return START_STICKY;
}

private void isRestrictedAppRunning() {
    mAppProcessInfosList = am.getRunningAppProcesses();
    for (int i = 0; i < mAppProcessInfosList.size(); i++) {
        if (mAppProcessInfosList.get(i).processName
                .equals("com.android.phone")
                || mAppProcessInfosList.get(i).processName
                        .equals("com.android.email")
                || mAppProcessInfosList.get(i).processName
                        .equals("com.android.mms")) {
            isLockedAppRunning = true;
            Intent dialogIntent = new Intent(getBaseContext(),
                    TestActivity.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(dialogIntent);
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    this.threadDone = false;
}
}

这段代码来自我的虚拟应用程序。 在实际的应用程序中有很多条件,仍然需要实现很多东西。 但是,我所需要的实际上将以这种方式工作。


In the app that I mentioned in my question no default behavior was overridden. That was my mistake, I took it in wrong way. The same functionality can be achieved using Handler. In my app I used a service which starts on click of a button. Inside the service,, I had used handler. Below, I am posting the code snippet.

public class DialerService extends Service {
ActivityManager am;
List<RunningAppProcessInfo> mAppProcessInfosList;
private Runnable myRunnable;
boolean threadDone = true;
Handler mHandler;
boolean isLockedAppRunning = false;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

public void onCreate() {
    am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    mAppProcessInfosList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
    mHandler = new Handler();
    Log.v("Dialer Service", "onCreate called");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    myRunnable = new Runnable() {

        @Override
        public void run() {
            isRestrictedAppRunning();
        }
    };

    new Thread(new Runnable() {
        public void run() {
            while (threadDone) {

                try {
                    mHandler.post(myRunnable);
                } catch (Exception e) {

                }
            }
        }
    }).start();
    return START_STICKY;
}

private void isRestrictedAppRunning() {
    mAppProcessInfosList = am.getRunningAppProcesses();
    for (int i = 0; i < mAppProcessInfosList.size(); i++) {
        if (mAppProcessInfosList.get(i).processName
                .equals("com.android.phone")
                || mAppProcessInfosList.get(i).processName
                        .equals("com.android.email")
                || mAppProcessInfosList.get(i).processName
                        .equals("com.android.mms")) {
            isLockedAppRunning = true;
            Intent dialogIntent = new Intent(getBaseContext(),
                    TestActivity.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(dialogIntent);
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    this.threadDone = false;
}
}

This code is from my dummy app. There are lot more conditions in actual app and still lots of things need to implemented. But, what I needed actually will work in this way.

相关问答

更多
  • 你可以像修改windowNoTitle那样的属性来覆盖标准属性,只是不要忘记像这样添加android:前缀: