How does Android customize the use of the server DynamicMockServer

Table of contents

quote

1. Pull the latest code of dynamicMockServer

2. Import the test interface configuration file.

3. Run the APP again to verify whether the service is availableEdit

4. Personalize the interface and return information you need

5. Dynamically return information according to access parameters

Six, custom interface and parameter verification configuration


Joint debugging with the server is often required during normal development, but server development often lags behind that of the front-end. At this time, we need to mock the data ourselves to adjust the process.

Today I will introduce a MockServer on Android----DynamicMockServer, which supports interface calls and static files.

DynamicMockServer:

  • Flexible configuration of interfaces that require mocks.
  • Dynamically modify the information returned by the interface. (Just push the new return information to the sdcard, no need to restart the app)
  • Support return of static files. (pictures, music, etc.)
  • Support MockServer service background resident.

quote

implementation 'com.gitee.gggl:dynamic-mock-server:v1.0'

1. Pull the latest code of dynamicMockServer

1、git clone https://gitee.com/gggl/dynamic-mock-server.git

2. Android Studio imports dynamic-mock-server

Engineering structure:

1. app -> test project, rely on and start the mockserver service.

2、mockserver -> http mock server

2. Import the test interface configuration file.

1. Run the app, and create a configuration file directory in the sdcard by default.

/sdcard/Android/data/com.macoli.dynamicmockserver/files/configuration_test

2. Import the configuration file used for testing.

The configuration file used by the test is in the (mock_server/configuration_test) directory of the project. 

Import all the files under mock_server/configuration_test to the phone's /sdcard/Android/data/com.macoli.dynamicmockserver/files/configuration_test directory.

File directory analysis:

  • The mock_server_config.json file defines the basic information of the mockserver and the interface information we need to support. (get, post, and static are the content that our interface needs to return)
  • The files in the get directory correspond to the information returned by all get request interfaces.
  • The files in the post directory correspond to the interface return information of all post requests
  • The static directory corresponds to all static resources (music, pictures) that need to be requested

3. Run the APP again to verify whether the service is available

Run the app again, click the static button, and two pictures will be displayed by default. As shown below. It means that our service has started normally.

4. Personalize the interface and return information you need

Open the mock_server_configuration.json configuration file. This file defines the server configuration and the interface information we need.

port: defines the server port.

base_path: Define the sdcard root directory used by our server. Subsequent get, post, and static return information are all in this base_path directory.

get array: define all get requests

uri: Indirect URI

default_path: The interface returns the information file path. For example, the login interface: our default return information is the get/login file (the get directory is automatically added by the code) and finally our login file is placed in /sdcard/Android/data/com.macoli.dynamicmockserver/files/configuration_test/get/login

static: Indicates whether this resource is a static resource. If it is a static resource, mockserver will search for the corresponding file in the static directory by default.

You only need to configure the interface you need according to the provided test configuration information format . After the configuration is complete, push the mock_server_configuration.json back to the phone's /sdcard/Android/data/com.macoli.dynamicmockserver/files/configuration_test/ directory. Note: If you modify base_path here, push according to your modified directory.

5. Dynamically return information according to access parameters

 For example, if you enter a user name and password in the login interface, we need to dynamically determine whether the login is successful based on the user name and password, and return different prompts.

dynamic-mock-server supports us to add destnation (final routing point) to support the function of dynamically judging parameters.


    private fun loginDestnation(destnationList : ArrayList<Destnation>) {
        val destnation : Destnation = Destnation()
        destnation.uri = "/login"
        destnation.destnation = "/login_success"
        val loginPredicate : (params : Map<String, List<String>>) -> Boolean = {
            val a : Boolean = (it.get("username")?.get(0) == "menshen")
            val b : Boolean = (it.get("password")?.get(0) == "woshildx")
            a && b
        }
        destnation.predicate = loginPredicate
        destnation.destnation = "/login_success"
        destnationList.add(destnation)
    }

The Destnation entity corresponds to a get or post request we just configured, but there will be one more predicate for judgment and verification. The predicate is a function that returns a boolean value, which needs to be implemented by the developer. The predicate receives the input parameters of the url input.

The input parameter of the predicate is the parameter entered when our interface is accessed. Example: http://127.0.0.1:8080/login?username=menshen&password=123456

The corresponding params will have two keys, username and password, and corresponding values. We can determine whether to return login_success by judging whether the username and password are legal. In this example, if the verification of menshen and 123456 fails, the default result defined in our configuration file will be returned, otherwise the file content corresponding to login_success will be returned.

Test in browser: http://127.0.0.1:8080/login?username=menshen&password=123456

We will see the result of login faild

Test in browser: http://127.0.0.1:8080/login?username=menshen&password=woshildx

We will see the results of Welcom XXX

At this time, we open the login and login_success files, and you will find that these two results are pre-defined in the contents of these two files.

Six, custom interface and parameter verification configuration

Now you can customize your mock_server by modifying mock_server_config.json.

Note that after the modification is completed, the file needs to be pushed to the corresponding directory of the mobile phone.

When you want to change the returned result, you only need to change the content of the file and push it back to the phone again, without re-running the project.

Guess you like

Origin blog.csdn.net/mldxs/article/details/128067198